Initialization of class member variables in C++ with static or const keywords. Define the following variables in a simple C++ class:
After adding the qualifier, there are four more cases. The following four cases are analyzed separately.
- #include <iostream>
- using namespace std;
- class TestVariable{
- public:
- TestVariable(){}
- private:
- int intVariable;//Case 0: No qualifier, this need not be discussed
- const int constIntVariable;//Case 1: const constant
- static int staticIntVariable;//Case 2: Static variables
- static const int staticConstIntVariable;//Case 3 Static constant type
- static const float staticConstNotIntVariable;//Case 4: Static very integer type
- };
- int main(){
- TestVariable variable;
- return 0;
- }
Case 1: const Modification
Compiling the above code, first of all, there is a problem in case 1. The error hint in g++ is:
The prompt in vs is more obvious, "error C2758:" TestVariable::constIntVariable": must be initialized in the list of constructor base/member initializers. The solution is given directly, that is to say, for const constant types in classes, initialization needs to be done in the constructor initialization list.
In this way, the problem of case 1 is solved.
- #include <iostream>
- using namespace std;
- class TestVariable{
- public:
- TestVariable()<span style="color:#ff0000;">:constIntVariable(0)</span>{}
- private:
- int intVariable;//Case 0: No qualifier, this need not be discussed
- const int constIntVariable;//Case 1: const constant
- static int staticIntVariable;//Case 2: Static variables
- static const int staticConstIntVariable;//Case 3 Static constant type
- static const float staticConstNotIntVariable;//Case 4: Static very integer type
- };
- int main(){
- TestVariable variable;
- return 0;
- }
Situation 2: static Modification
Then compile the above code to pass, but obviously there is a problem that the static variable is not initialized, but there is no error in compiling. The reason is that these variables have not been used yet, because the static variables are shared by all instances of the class, so constIntVariable is only checked when the variable is constructed. The compilation error occurs, but the remaining three static variables do not see the problem because they are not used all. Modify the code to check for static variables in case 2:
Errors in g++ compilation are as follows:
- #include <iostream>
- using namespace std;
- class TestVariable{
- public:
- TestVariable():constIntVariable(0){}
- <span style="color:#ff0000;">void printStaticInt(){
- cout<<"staticIntVariable:"<<staticIntVariable<<endl;
- }</span>
- private:
- int intVariable;//Case 0: No qualifier, this need not be discussed
- const int constIntVariable;//Case 1: const constant
- static int staticIntVariable;//Case 2: Static variables
- static const int staticConstIntVariable;//Case 3 Static constant type
- static const float staticConstNotIntVariable;//Case 4: Static very integer type
- };
- int main(){
- TestVariable variable;
- variable.printStaticInt();
- return 0;
- }
The error prompt in GS is "error LNK2001: the external symbol"private: static int Test Variable:: static IntVariable"(? Static IntVariable@TestVariable@@0HA). In this case, the prompt in g++ is more obvious, that is, an undefined reference to the variable TestVariable:: static IntVariable, so our solution is to initialize it. Initialization of this variable operates as follows:
- #include <iostream>
- using namespace std;
- class TestVariable{
- public:
- TestVariable():constIntVariable(0){}
- void printStaticInt(){
- cout<<"staticIntVariable:"<<staticIntVariable<<endl;
- }
- private:
- int intVariable;//Case 0: No qualifier, this need not be discussed
- const int constIntVariable;//Case 1: const constant
- static int staticIntVariable;//Case 2: Static variables
- static const int staticConstIntVariable;//Case 3 Static constant type
- static const float staticConstNotIntVariable;//Case 4: Static very integer type
- };
- <span style="color:#ff0000;">int TestVariable::staticIntVariable=1;</span>
- int main(){
- TestVariable variable;
- variable.printStaticInt();
- return 0;
- }
Note that int TestVariable::staticIntVariable=1; this line of code cannot be written as static int TestVariable::staticIntVariable=1; otherwise this error will occur in g++:
In vs, the error C2720: "TestVariable::staticIntVariable": the "static" storage class descriptor on the member is illegal is an error prompt.
Case 3: static const modified integer data
Then there's case 3 and case 4, where the qualifier is the same as case 3 and case 4, which is static const (written here as static const and const static seems to work, at least in vs and g++). test All pass, but the difference is the data type, where int type is a special case, that is, static constant integer (short). Int, long, long) data can be initialized in a class, while other types can only be initialized outside the class.
First, add a function that calls this variable:
- #include <iostream>
- using namespace std;
- class TestVariable{
- public:
- TestVariable():constIntVariable(0){}
- void printStaticInt(){
- cout<<"staticIntVariable:"<<staticIntVariable<<endl;
- }
- <span style="color:#ff0000;">void printStaticConstInt(){
- cout<<"staticOnstIntVariable:"<<staticConstIntVariable<<endl;
- }</span>
- private:
- int intVariable;//Case 0: No qualifier, this need not be discussed
- const int constIntVariable;//Case 1: const constant
- static int staticIntVariable;//Case 2: Static variables
- static const int staticConstIntVariable;//Case 3 Static constant type
- static const float staticConstNotIntVariable;//Case 4: Static very integer type
- };
- int TestVariable::staticIntVariable=1;
- int main(){
- TestVariable variable;
- variable.printStaticInt();
- variable.printStaticConstInt();
- return 0;
- }
The result of compiling in g++ is as follows:
Error compiled with vs: "error LNK2001: The unresolved external symbol"private: static int const TestVariable:: static Const IntVariable"(? Static Const IntVariable@TestVariable@@0HB)) is the same as the error prompt in case 2.
There are only two ways to define it, in the class definition:
Define outside the class:
- #include <iostream>
- using namespace std;
- class TestVariable{
- public:
- TestVariable():constIntVariable(0){}
- void printStaticInt(){
- cout<<"staticIntVariable:"<<staticIntVariable<<endl;
- }
- void printStaticConstInt(){
- cout<<"staticOnstIntVariable:"<<staticConstIntVariable<<endl;
- }
- private:
- int intVariable;//Case 0: No qualifier, this need not be discussed
- const int constIntVariable;//Case 1: const constant
- static int staticIntVariable;//Case 2: Static variables
- <span style="color:#ff0000;">static const int staticConstIntVariable=3;//Case 3 Static constant type</span>
- static const float staticConstNotIntVariable;//Case 4: Static very integer type
- };
- int TestVariable::staticIntVariable=1;
- int main(){
- TestVariable variable;
- variable.printStaticInt();
- variable.printStaticConstInt();
- return 0;
- }
Note that the keyword const is also needed here, otherwise there will be an error in g++: it will treat it as a new variable, but the variable name is the same as that of the original const type, so it will make a mistake.
- #include <iostream>
- using namespace std;
- class TestVariable{
- public:
- TestVariable():constIntVariable(0){}
- void printStaticInt(){
- cout<<"staticIntVariable:"<<staticIntVariable<<endl;
- }
- void printStaticConstInt(){
- cout<<"staticOnstIntVariable:"<<staticConstIntVariable<<endl;
- }
- private:
- int intVariable;//Case 0: No qualifier, this need not be discussed
- const int constIntVariable;//Case 1: const constant
- static int staticIntVariable;//Case 2: Static variables
- static const int staticConstIntVariable;//Case 3 Static constant type
- static const float staticConstNotIntVariable;//Case 4: Static very integer type
- };
- int TestVariable::staticIntVariable=1;
- <span style="color:#ff0000;"> const int TestVariable::staticConstIntVariable=3;</span>
- int main(){
- TestVariable variable;
- variable.printStaticInt();
- variable.printStaticConstInt();
- return 0;
- }
The prompt in vs is "error C2373:" static Const Int Variable: redefinition; different type modifiers".
Case 4: static const modified non-integer data
Then case 4, for static constant non-integer data, if initialization is done within the class:
No problem in g++:
- #include <iostream>
- using namespace std;
- class TestVariable{
- public:
- TestVariable():constIntVariable(0){}
- void printStaticInt(){
- cout<<"staticIntVariable:"<<staticIntVariable<<endl;
- }
- void printStaticConstInt(){
- cout<<"staticOnstIntVariable:"<<staticConstIntVariable<<endl;
- }
- void printStaticConstNotIntVariable(){
- cout<<"staticConstNotIntVariable:"<<staticConstNotIntVariable<<endl;
- }
- private:
- int intVariable;//Case 0: No qualifier, this need not be discussed
- const int constIntVariable;//Case 1: const constant
- static int staticIntVariable;//Case 2: Static variables
- static const int staticConstIntVariable;//Case 3 Static constant type
- <span style="color:#ff0000;">static const float staticConstNotIntVariable=4.0;//Case 4: Static very integer type</span>
- };
- int TestVariable::staticIntVariable=1;
- const int TestVariable::staticConstIntVariable=3;
- int main(){
- TestVariable variable;
- variable.printStaticInt();
- variable.printStaticConstInt();
- variable.printStaticNotIntVariable();
- return 0;
- }

This error "error C2864:" TestVariable:: static ConstNotIntVariable"will appear in vs 2008: Only static constant integer data members can be initialized in the class.
So it's better not to write this code.
Putting the initialization of variables outside the class is as good as case 3.
- #include <iostream>
- using namespace std;
- class TestVariable{
- public:
- TestVariable():constIntVariable(0){}
- void printStaticInt(){
- cout<<"staticIntVariable:"<<staticIntVariable<<endl;
- }
- void printStaticConstInt(){
- cout<<"staticOnstIntVariable:"<<staticConstIntVariable<<endl;
- }
- void printStaticConstNotIntVariable(){
- cout<<"staticConstNotIntVariable:"<<staticConstNotIntVariable<<endl;
- }
- private:
- int intVariable;//Case 0: No qualifier, this need not be discussed
- const int constIntVariable;//Case 1: const constant
- static int staticIntVariable;//Case 2: Static variables
- static const int staticConstIntVariable;//Case 3 Static constant type
- static const float staticConstNotIntVariable;//Case 4: Static very integer type
- };
- int TestVariable::staticIntVariable=1;
- const int TestVariable::staticConstIntVariable=3;
- <span style="color:#ff0000;">const float TestVariable::staticConstNotIntVariable=4.0;</span>
- int main(){
- TestVariable variable;
- variable.printStaticInt();
- variable.printStaticConstInt();
- variable.printStaticConstNotIntVariable();
- return 0;
- }
This is the case in g++ and vs run the results are correct.
Finally, with regard to static, it has different meanings in and out of classes. Here's a summary:
1. The static variable in the function body acts as the function body. Unlike the auto variable, the memory of the variable can only be allocated once, so its value remains the last value when it is called next time.
2. static global variables within a module can be accessed by all functions within the module, but not by other functions outside the module.
3. static functions within a module can only be called by other functions within the module, and the scope of use of this function is limited to the module that declares it.
4. static member variables in a class belong to the whole class, and only one copy of all objects in the class.
5. The static member function in the class belongs to the whole class. This function does not accept this pointer, so it can only access the static member variable of the class.