Static member notes of C + + classes

Time:2022-5-15

Here are the static member notes of C + + classes.

Static data member characteristics

Declare with keyword static

Shared by all objects of this class, static data members have static lifetime

Must be defined and initialized outside the class. Use (::) to indicate the class to which it belongs

Example – point class with static data members

Code example:

1 #include
 2 
 3 using namespace std;
 4 
 5 class point // point class definition
 6 {
 7 public: / external interface
 8 point (int x = 0, int y = 0): X (x), y (y) // constructor
 9         {
10             count++;
11         }
12 
13 point (point & P) // copy constructor
14         {
15             x = p.x;
16             y = p.y;
17             count++;
18         }
19 
20 ~point() // destructor, called before main function return returns
21         {
22             count--;
23         }
24 
25         int getX()
26         {
27             return x;
28         }
29 
30         int getY()
31         {
32             return y;
33         }
34 
35         void showCount()
36         {
37             cout << "Object count = " << count << endl;
38         }
39 
40 private: / private data member
41         int x,y;
42         static int count;  // Static data member
43 };
44 
45 int Point::count = 0; // Static data member definition and initialization, qualified by class name
46 
47 int main(void)
48 {
49     Point a(4,5);
50     cout << "Point A: " << a.getX() << "," << a.getY();
51     a.showCount(); // Number of output objects
52 
53     Point b(a); // Copy the constructor call and define object B, whose constructor will make count++
54     cout << "Point B:" << b.getX() << "," << b.getY();
55     b.showCount();
56 
57 
58     return 0;
59 }

Operation results:

1 Point A: 4,5Object count = 1

2 Point B:4,5Object count = 2 

Static function member characteristics

Out of class code can use the class name and scope operator to call static member functions

The static member function is mainly used to process the static data members of this class. You can call the static member function directly

If you access non static members, you need to access them through objects

Example – point class with static data / function members

Code example:

1 #include
 2 
 3 using namespace std;
 4 
 5 class point // point class definition
 6 {
 7 public: / external interface
 8 point (int x = 0, int y = 0): X (x), y (y) // constructor
 9         {
10             count++;
11         }
12 
13 point (point & P) // copy constructor
14         {
15             x = p.x;
16             y = p.y;
17             count++;
18         }
19 
20 ~point() // destructor, called before main function return returns
21         {
22             count--;
23         }
24 
25         int getX()
26         {
27             return x;
28         }
29 
30         int getY()
31         {
32             return y;
33         }
34 
35 static void showcount() // static function members
36         {
37             cout << "Object count = " << count << endl;
38         }
39 
40 private: / private data member
41         int x,y;
42         static int count;  // Static data member
43 };
44 
45 int Point::count = 0; // Static data member definition and initialization, qualified by class name
46 
47 int main(void)
48 {
49     Point::showCount();
50 
51     Point a(4,5);
52     cout << "Point A: " << a.getX() << "," << a.getY();
53 
54     Point::showCount();
55     //a.showCount(); // You can also output several objects
56 
57     Point b(a); // Copy the constructor call and define object B, whose constructor will make count++
58     cout << "Point B:" << b.getX() << "," << b.getY();
59 
60     Point::showCount();
61     //b.showCount();// You can also output several objects
62 
63 
64     return 0;
65 }

Operation results:

1 Object count = 0

2 Point A: 4,5Object count = 1

3 Point B:4,5Object count = 2