The static member variables and const constants of the C ++ class must be initialized when defining the class; otherwise, compilation errors will occur.
The initialization method is as follows:
C ++ class static member variable Initialization Method
# Include <iostream> # include <string> # include <cstdio> # include <cstring> using namespace std; class A {public: static void fun () {AB = 2; cout <AB <endl;} private: static int AB;}; int A: AB = 10 // initialize int main (int argc, char * argv []) here {A: fun (); return 0 ;}# include <iostream >#include <string >#include <cstdio >#include <cstring> using namespace std; class A {public: static void fun () {AB = 2; cout <AB <endl;} private: static int AB ;}; int :: AB = 10 // initialize int main (int argc, char * argv []) {A: fun (); return 0 ;}
C ++ class const constant initialization method:
# Include <iostream> # include <string> # include <cstdio> # include <cstring> using namespace std; class A {public: A: AB (10) // initialize AB {;} static void fun () {AB = 2; cout <AB <endl;} const int AB;} here ;}; # include <iostream> # include <string> # include <cstdio> # include <cstring> using namespace std; class A {public: A: AB (10) // initialize AB {;} static void fun () {AB = 2; cout <AB <endl;} const int AB;} here ;};