knowledge Point Class static data member class static member function a static member of a class static data member is proposed to solve the problem of data sharing. There are many ways to implement sharing, such as setting a global variable or object as a method. However, there are limitations to global variables or objects. In this lesson, we mainly discuss the use of static members of the class to achieve the sharing of data between objects. The use of static data members and considerations are as follows: 1. Static data members precede the definition or description with the keyword static. 2, static member initialization differs from general data member initialization. The static data member is initialized in the following format: < data type >< class name >::< static data member name >=< value > 3, static data member is statically stored, it is a static lifetime, It must be initialized. 4.when referencing static data members, use the following format: < class name >::< static member name > Second, class static member function cannot use non-static class member data within static member functions. class member functions can be used independently. You can call a static member function using the class name + scope Resolver. such as Tdate::staticfun (); It is best to place the implementation of the member function inside the. cpp to prevent duplicate definitions.
Header file
classtdate{Private: inttest; intFun (); Public: intYear//years intMonth//Month intDay//Day voidSetData (intYintMintd); voidPrintDate ()Const; Tdate ();//default constructorTdate (Char*s); Tdate (intYintMintd); intGettest ()Const; voidTdate::setcount (intvalue); Static voidStaticfun (); //defines a static member variable that holds the number of instances of the class (number of objects) Static intcount; voidSettest (intv) {test=v; } tdate*Getthis () {return This;//return Object Address} friendintTfun (tdate d1);}; InlineintTdate:: Gettest ()Const{ returntest;}
Class implementation file
#include"stdafx.h"#include"date.h"intTdate::count=0;//class static member initializationvoidTdate::setdata (intYintMintBD) { Year=y; Month=m; Day=D;}voidTdate::p rintdate ()Const{printf ("%d:%d:%d\n", tdate::year,tdate::month,tdate::d ay);}//default constructortdate::tdate () {//Initialize CodeCount++;//Countprintf"Enter the default constructor \ n"); //tdate::year=1990; This->year=1990; This->month=0; This->day=0; printf ("this=%x \ n", This);} Tdate::tdate (Char*s) {Count++;//Countprintf"%s", s); year=1990; Month=0; Day=0;}//constructor FunctionTdate::tdate (intYearintMonthintDay ) { //Initialize Codecount++;//Countprintf"enter constructor \ n"); This->year=year;//y=y This->month=month; This->day=Day ; PrintDate ();}intTdate::fun () {return 1;}voidTdate::setcount (intvalue) {Tdate::count=value;}voidTdate::staticfun () {printf ("%d", count);}
Class Call File
//helloc++.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<malloc.h>#include"date.h"intTfun (tdate d1) {d1.fun (); intR=d1.test; return 1;}int_tmain (intARGC, _tchar*argv[]) { intb[3]={1,2,3}; //tdate A (1999,1,19);tdate d1,d2; printf ("%d", Tdate::count); Tdate::staticfun (); D2.printdate (); /*D2.setcount (7777); D1.setcount (888); printf ("d2.count=%d \ n", Tdate.count); printf ("%x,%x,%x", &d1.count,&d2.count, &tdate::count);*/GetChar (); return 0;}
MFC class static Members