Code:
1#include <iostream>2#include <string>3#include <cstdio>4 5 using namespacestd;6 7 classa{8 Public:9 Static intA;Ten //static int a = 0;//compilation does not pass and cannot be initialized within a class One intb; A Static voidfunc1 () { -cout<<"static Func"<<Endl; -a++; the //b++; - } - voidFunc2 () { -cout<<"normal func"<<Endl; +a++; -b++; + } A }c; at intA::a =0;//compilation can be initialized outside of the class - - intMainintargcChar*argv[]) { - - c.func1 (); - C.func2 (); in - return 0; to}
Output:
Static Funcnormal func
Analysis:
1. Static member variables cannot be initialized in a class and need to be initialized outside of the class.
2, static member functions can only access static member variables, ordinary member functions do not have this restriction.
3. A static member function cannot be a virtual function.
The use of static member variables and static member functions in C + + classes