cpp:类中的静态成员变量和静态成员函数(class)
一、类中的静态成员变量和静态成员函数:
1、类中的静态成员变量和静态成员函数:它们属于‘类’不属于‘对象’, 它们没有指针‘ this ’。
2、类中的静态成员变量和静态成员函数:静态成员变量,只能在类外初始化;静态成员函数只能访问静态成员变量、静态成员函数以及与本类无关的函数;
3、伪代码:
1 #include<iostream>
2
3
4 using namespace std;
5
6
7 class information
8 {
9 public:
10 information(){ count++;}
11 information(int e){ count++; elem=e;}
12 ~information(){ cout << "OS: ~information()" << endl; }
13 void set(int e){elem=e;}
14 int get(){ return elem;}
15 void print_count()
16 {
17 // using static member count
18 cout << "print_count = " << count << endl;
19 }
20 static void msg();
21 static void count_object();
22 private:
23 int elem;
24 static int count;
25 };
26
27
28 // define static function msg(); no keyword 'static'
29 void information::msg()
30 {
31 cout <<"\nvoid information::msg()\n" << endl;
32 }
33
34
35 // define static function count_object(), no keyword 'static'
36 void information::count_object()
37 {
38 // first: using static count
39 // cout << "\ninformation_object = " << information::count << "\n" << endl;
40
41 // second: using static count
42 cout << "\ninformation_object = " << count << "\n" << endl;
43 }
二、应用示例
1 [root@rockylinux tmp]# cat static_member_function_class.cpp
2 #include<iostream>
3
4
5 using namespace std;
6
7
8 class information
9 {
10 public:
11 information(){ count++;}
12 information(int e){ count++; elem=e;}
13 ~information(){ cout << "OS: ~information()" << endl; }
14 void set(int e){elem=e;}
15 int get(){ return elem;}
16 void print_count()
17 {
18 // using static member count
19 cout << "print_count = " << count << endl;
20 }
21 static void msg();
22 static void count_object();
23 private:
24 int elem;
25 static int count;
26 };
27
28
29 // define static function msg(); no keyword 'static'
30 void information::msg()
31 {
32 cout <<"\nvoid information::msg()\n" << endl;
33 }
34
35
36 // define static function count_object(), no keyword 'static'
37 void information::count_object()
38 {
39 // first: using static count
40 // cout << "\ninformation_object = " << information::count << "\n" << endl;
41
42 // second: using static count
43 cout << "\ninformation_object = " << count << "\n" << endl;
44 }
45
46
47 // static variable initial; no keyword 'static'
48 int information::count = 0;
49
50
51 // using static member function
52 void use_static_member_function()
53 {
54 // using static member function
55 information::count_object();
56 }
57
58
59 // create information object , oject number is 'size'.
60 information* create_information_object(int size)
61 {
62 information* pointer_information;
63 information object_array[size];
64 pointer_information = object_array;
65
66 return pointer_information;
67 }
68
69
70 // use the function 'print_count()' of an object array of information.
71 void use_information_object(information* pointer_information, int size)
72 {
73 cout <<"\n"<< endl;
74 for(int i=0; i<size; i++)
75 {
76 pointer_information -> print_count();
77 pointer_information++;
78 }
79 cout <<"\n"<< endl;
80
81 }
82
83
84 // test part.
85 int main(int argc, char* argv[], char* envp[])
86 {
87
88 // print count, when it initializes.
89 // information::count_object();
90 use_static_member_function();
91
92 // information create 10 objects.
93 information* ptinfo ;
94 ptinfo = create_information_object(10);
95 use_information_object(ptinfo, 10);
96
97 // print count, after information creates 10 ojects.
98 // information::count_object();
99 use_static_member_function();
100
101 return 0;
102 }
103 [root@rockylinux tmp]#
104 [root@rockylinux tmp]#
105 [root@rockylinux tmp]# ./static_member_function_class
106
107 information_object = 0
108
109 OS: ~information()
110 OS: ~information()
111 OS: ~information()
112 OS: ~information()
113 OS: ~information()
114 OS: ~information()
115 OS: ~information()
116 OS: ~information()
117 OS: ~information()
118 OS: ~information()
119
120
121 print_count = 10
122 print_count = 10
123 print_count = 10
124 print_count = 10
125 print_count = 10
126 print_count = 10
127 print_count = 10
128 print_count = 10
129 print_count = 10
130 print_count = 10
131
132
133
134 information_object = 10
135
136 [root@rockylinux tmp]#
137 [root@rockylinux tmp]#