Static member of C + + -- a detailed discussion on static decorated member

First of all, we understand it literally

Member: member variable, member function.

static modifies member variables, as well as member functions.

Static is declared static and is called a static member. No matter how many objects this class creates, there is only one copy of static members, which is shared by all objects belonging to this class.

A static member of a class rather than an object.

 

 

1.static decorated member variable

Static variables allocate space in the compilation phase. When an object has not been created, it already allocates space

Static member variables must be declared in the class and defined outside the class.

The static data member does not belong to an object, and the space occupied by the static member is not included in the space allocated for the object.

 1 class Data
 2 {
 3 public:
 4     int num;//Common member variable
 5     static int data;//Static member variable (declared in class)
 6 };
 7 //No need to add when defining static
 8 int Data::data=100;//Off class definition+Initialization
 9 
10 void tesk07()
11 {
12     //data It is a static member variable and belongs to a class. You can use the class name::Direct access
13     cout<<Data::data<<endl;//100
14     //assignment
15     Data::data = 200;
16     cout<<Data::data<<endl;//200
17 
18     //data Static variables are shared by all objects and can be accessed through the object name
19     Data ob1;
20     ob1.data = 300;
21     cout<<Data::data<<endl;//300
22 
23     Data ob2;
24     cout<<ob2.data<<endl;//300
25 
26     //Common member variable, belonging to the object, can only be accessed through the object name
27     ob1.num = 100;
28     cout<<"ob2.num="<<ob2.num<<endl;//Random value
29     //cout<<Data::num<<endl;//Common member variables cannot be accessed by class name
30 }

2. Static decorated static member function

 1 class Data
 2 {
 3 private:
 4     int num;//Common member variable
 5     static int data;//Static member variable (declared in class)
 6 public:
 7     //Common member function, which depends on the required object call of the object
 8     int getData(void)
 9     {
10         return data;
11     }
12 };
13 
14 //No need to add when defining static
15 int Data::data = 100;//Off class definition+Initialization
16 
17 void tesk08()
18 {
19     //cout<<Data::data<<endl;//err static state data It is private and cannot be accessed directly outside the class
20     //cout<<Data::getData()<<endl;//err getData() Must object call
21     
22     Data ob;
23     cout<<ob.getData()<<endl;
24     //Existing problems:data Static, existing before the object is created
25 }
There is a problem: data is static and exists before the object is created
    Can't data be used if the class doesn't instantiate the object? 
    To solve these problems, we need to use static member function

Static member function:
 1 class Data
 2 {
 3 private:
 4     int num;//Common member variable
 5     static int data;//Static member variable (declared in class)
 6 public:
 7     //Common member function, which depends on the required object call of the object
 8     int getData(void)
 9     {
10         return data;
11     }
12     
13     //Static member function, belonging to class, not object
14     static int getDataStatic(void)
15     {
16         return data;
17     }
18 };
19 
20 //No need to add when defining static
21 int Data::data = 100;//Off class definition+Initialization
22 
23 void tesk08()
24 {
25     //cout<<Data::data<<endl;//err static state data It is private and cannot be accessed directly outside the class
26     //cout<<Data::getData()<<endl;//err getData()Must be an object call
27     
28     Data ob;
29     cout<<ob.getData()<<endl;
30     
31     //1.Static member function, belonging to a class, can be accessed directly through the class name
32     cout<<Data::getDataStatic()<<endl;
33     
34     //2.Can also be accessed by object name (object shares static member functions)
35     cout<<ob.getDataStatic()<<endl;
36 }

Be careful:

1. The purpose of static member function is to operate static member data.

2. Static member function, unable to access non static member data.

 

 

3. Ordinary member functions can operate static member data and non static member data.

 


4. Both static member variables and static member functions have permissions

-------------------------------
3.const decorated static members
If the members of a class need to realize both sharing and immutability, they should be decorated with static const
 1 class Data
 2 {
 3 public:
 4     const static int data;//Static member variable(Within class declaration)
 5 public:
 6     //Static member functions belong to classes but not objects
 7     static int getDataStatic(void)
 8     {
 9         //num = 200;//err Static member functions cannot access common member variables
10         return data;
11     }
12 };
13 
14 //No need to add when defining static
15 const int Data::data=100;//Off class definition+Initialization
16 
17 void test02()
18 {
19     //Visit
20     cout<<Data::data<<endl;
21     //assignment
22     //Data::data = 200;//err data Static member read only
23     cout<<Data::data<<endl;
24 }

Tags: C++

Posted on Thu, 19 Mar 2020 08:34:52 -0700 by rar_ind