Members in a class can be divided into ordinary and static members, and functions can be divided into ordinary functions and static functions. Ordinary members can only be owned by each object alone, whereas static members are common to all objects of the class. In a static member function, only static member variables can be called, and normal member variables cannot be called.
For example:
1 classMyData2 {3 Public:4MyDataintAintB) {a=a;b=B;}5~MyData () {};6 void StaticFun ()7 {8cout<<"C:"<<c<<endl;//Legal. Static member variables can and can only be called in static functions9 //cout<< "A:" <<a<<endl;//illegal! Normal member variables cannot be called in a static functionTen } One A voidGeta () {cout<<"A:"<<a<<Endl;} - voidGETB () {cout<<"B:"<<b<<Endl;} - voidGetC () {cout<<"C:"<<c<<Endl;} the - Private: - intA; - intb; + Static intC; - }; + A intMydata::c =5;//a description of the definition of a static member variable at - intMain () - { -MyData D1 (3,4); -D1.fun ();//invoking a static member function of a class through an object -Mydata::fun ();//calling a static member function of a class through a class name in - return 0; to}
The static keyword in C + +