//static member variables in a class, static member functions#define_crt_secure_no_warnings#include<iostream>using namespacestd;/*A static member function is a variable that belongs to the entire class, static modifiers, belongs to a class, and all objects can be shared. In a static data member function of a class, the property of a variable that cannot invoke a specific object is because the static modified variable is the entire class share, using the member property of an object in a static member function C + + The compiler cannot know which object this member property is, so only static member variables can be used in static member functions*/classpoint{ Public: Point (int_x,int_y) {cout<<"The custom parameter constructor was called 1"<<Endl; X=_x; Y=_y; } Point&pm) {cout<<"I am a custom copy constructor! "<<Endl; } ~Point () {cout<<"The custom destructor was called 3"<<Endl; } Static voidGetnum () {cout<< Z <<Endl; } Static voidSetnum (intc) {Z=C; }Private: intx; inty; //Static member variables Static intz;};//static member initialization of a class (correct example)intPoint::z =Ten;voidProtecta () {//Point P1 (); //Warning Warning C4930: "point P1 (void)": The prototype function is not invoked (is it intentionally defined with a variable?) //Analysis: By observing that this line of code C + + compilation does not execute at all, to call the parameterless constructor point P1;//The call copy constructor should use Point P1 (p2); //Point P2 (1,1,2); //Error LNK2001: unresolved external symbol "Private:static int point::z" ([email protected]@@0ha)//This indicates that the static member variable of the class cannot be initialized with the constructor function//static member initialization of a class (error example)//int point::z = 0; //Error C2655: "point::z": defined or re-declared illegal in the current scope//initializing a static member of a class within a function is considered to be a member variable that defines a class//to print the value of a static member variable//method of calling a static member function 1Point::getnum (); //method of calling a static member function 2Point P3 (2,2); P3. Setnum ( One); Point::getnum (); P3. Getnum ();}voidMain () {protecta (); System ("Pause");}
Static member variables in C + + classes, static member functions