C + + class static members and class static member functionsWhen a data member of a class is declared static, the static data member can only be defined once, and is shared by all objects of the same kind. Each object has a copy of every regular data member in the class, but only one instance of the static data member exists, regardless of how many class objects are defined. A static method is related to the class and is a behavior of the class, not related to the instance object of the class. One of the uses of static data members is to count how many objects actually exist. Static data members cannot be initialized in a class, but the class definition is simply a blueprint for describing the object in which the initial value is not allowed. The member cannot be initialized in the constructor of the class, because the static data member is shared by the objects of the class, or the static data members are reinitialized each time the object of the class is created. A static member cannot be assigned in the class body because it is shared by all objects of that class. You assign a value to it in an object, and the member in the other object changes. To avoid confusion, it is not necessary to assign values in the class body.
The value of a static member is the same for all objects. Static members can be initialized, but can only be initialized outside of the class body. General form: Data type class Name:: static data member name = initial value Note: You cannot initialize a static member with a parameter initialization table. The general system defaults initially to 0.
A static member is a shared member of all objects of a class, not a member of an object. It does not occupy storage space in the object, which is common to the entire class and does not belong to any particular object. So static members cannot initialize within classes, such as declaring a student class, where one member is the total number of students, then the variable should be declared as a static variable and the member variable should be set according to the actual requirements.
#include "iostream" using namespace std; Class Test {private: int x; int y; public: static int num; &N bsp; static int getnum () { x+=5; //This line of code is wrong mistakenly, static member functions cannot invoke non-static data members, which are invoked through the object of the class. num+=15; return num; }}; int test::num = 10; int main (void) { test A; cout<<test::num<<endl; & NBSP;//10 Test::num = 20; cout<<test::num<<endl; &NBSP;//20 cout<<test::getnum () <<endl; //35 Cout<<a.getnum () <<endl; &NBSP;&NBSP;//50 System ("pause"); return 0; } Through the above example know: x+=5; &nbSp This line of code is wrong a static function member must access a non-static data member through an object name. In addition, static member functions are not required to add the static keyword when implemented outside the class, or they are wrong. If you implement the static member function above in the body of the class, you cannot add static keyword, so write it: int test::getnum () { ... } 1, the owner of the static member is the class itself and the object, but many objects have the same static members. This defines the object as being unable to initialize it through a constructor. 2, static members can not be initialized in the class definition, can only be initialized outside of the class body. 3, static members still follow the public,private,protected access guidelines. 4. A static member function does not have the this pointer, and it cannot return non-static members because the class itself can be invoked in addition to the object calling it.
Static member functions can directly access the static data and function members of the class, while access to non-static data members must be passed by parameter to obtain an object name, which is then accessed through the object name.
Class Myclass
{
Private
int a,b,c;
static int Sum; Declaring static data members
Public
Myclass (int a,int b,int c);
void Getsum ();
};
int myclass::sum=0; Define and initialize static data members
Myclass::myclass (int a,int b,int c)
{
this->a=a;
this->b=b;
this->c=c;
Sum+=a+b+c;
}
void Myclass::getsum ()
{
cout << "sum=" <<sum <<endl;
}
int main (void)
{
Myclass Me (10,20,30);
Me. Getsum ();
System ("pause");
return 0;
}
The above example shows that non-static member functions can access static member functions and static data members arbitrarily.
Non-static member functions MyClass (int a,int b,int c) and Getsum () have access to static data member sum.
Static member functions do not have access to non-static member functions and non-static data members.
For static member functions, you can summarize the following points:
A function definition that appears outside the class body cannot specify a keyword static;
Static members can access each other, including static member functions accessing static data members and accessing static member functions;
Non-static member functions can access static member functions and static data members arbitrarily;
Static member functions do not have access to non-static member functions and non-static data members;
Because there is no extra overhead for this pointer, static member functions have a slight increase in speed compared to the global functions of a class;
Calling a static member function, you can use the member access operator (.) and (->) calls a static member function for an object of a class or a pointer to a class object.
When all objects of the same class use a quantity, a static data member variable is used for this shared amount, which takes the same value for all objects of the same class. Static member variables can only be called by static member functions. Static member functions are also shared by all objects in the same class. Only static member variables and static member functions can be invoked.