Analysis of member variables plus static or const keywords in C + +

Source: Internet
Author: User

Initialization problems with class member variables in C + + after the static or const keyword. Define the following variables in a simple C + + class:

#include <iostream>using namespace Std;class testvariable{public:testvariable () {}private:int intvariable;// Case 0 Without qualifier, this does not need to be discussed const int constintvariable;//Case 1 const constant static int staticintvariable;//case 2 static variable statics const INT staticconstintvariable;//condition 3 Static constant integer type statically const float staticconstnotintvariable;//condition 4 static very integral type};int main () { testvariable Variable;return 0;}
After the qualifier is added 4, the following four cases are analyzed separately.

Condition 1:const Modification

Compile the above code, first of all in case 1 problem, the error in g++ is:


The hint in VS is more obvious, "Error C2758:" testvariable::constintvariable ": Must be initialized in the constructor base/member initializer list." The workaround is given directly, meaning that the const constant type in the class needs to be initialized in the constructor initializer list .

#include <iostream>using namespace Std;class testvariable{public:testvariable () <span style= "color: #ff0000;" >:constintvariable (0) </span>{}private:int intvariable;//case 0 without qualifier, this does not need to be discussed const int constintvariable;// Scenario 1 const constant static int staticintvariable;//condition 2 static variable statics const int staticconstintvariable;//case 3 static constant integer type Statics const float staticconstnotintvariable;//Condition 4 static very integral type};int main () {testvariable Variable;return 0;}
In this case, the problem of 1 is solved.

Condition 2:static Modification

Then compile the above code to pass, but obviously there is a problem, that is, the static variable is not initialized, but there is no error at compile time. The reason is that these variables have not been used, because the static variable is shared by all instances of the class, so when constructing the variable variable, only the constintvariable variable is checked and the compile error occurs. But the remaining 3 static variables do not have to be used all the same to see the problem. Modify the code to check for static variables in Scenario 2:

#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 without qualifier, this does not need to be discussed const int constintvariable;//Case 1 const constant static int staticintvariable;//condition 2 static variable statics const int staticconstintvariable;//case 3 static constant integer type static const float staticconstnotintvariable;//condition 4 static very integral type};int main () {testvariable variable;variable.printstaticint (); return 0;}
g++ compile-time error conditions are as follows:



The error in VS is "error LNK2001: unresolved external symbol" Private:static int testvariable::staticintvariable "([email protected]@@0ha)", In this case, the hint in g++ is more obvious, that is, the reference to the testvariable::staticintvariable variable is undefined, so our solution is to initialize it. The initialization of this variable is done as follows:

#include <iostream>using namespace Std;class testvariable{public:testvariable (): constintvariable (0) {}void Printstaticint () {cout<< "staticintvariable:" <<STATICINTVARIABLE<<ENDL;} Private:int intvariable;//case 0 without qualifier, this does not need to be discussed const int constintvariable;//Case 1 const constant static int staticintvariable;// Condition 2 static variable statics const int staticconstintvariable;//condition 3 static constant integer type static const float staticconstnotintvariable;//condition 4 Static very integral type};<span style= "color: #ff0000;" >int Testvariable::staticintvariable=1;</span>int Main () {testvariable variable;variable.printstaticint () ; return 0;}

Note int testvariable::staticintvariable=1; this line of code cannot be written as a static int testvariable::staticintvariable=1;, otherwise this error will occur in g++:


In VS, "error C2720:" testvariable::staticintvariable ":" Static "storage class specifier illegal on member" is the wrong hint.

Case 3:static Const-Modified integer data

Then is the case 3 and Case 4, case 3 and the qualifier in case 4 is the same, that is, static const (this is written as static const and const static can be, at least in VS and g++ test passed), the difference is the data type, Here the int type is a special case, that is, the static constant integer (short, int, long,long long) data can be initialized in the class, while the other types can only be initialized outside the class.

First add the 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 without qualifier, this does not need to be discussed const int constintvariable;//Case 1 const constant static int staticintvariable;//condition 2 static variable statics const int staticconstintvariable;//case 3 static constant integer type static const float staticconstnotintvariable;//condition 4 static very integral type}; int Testvariable::staticintvariable=1;int Main () {testvariable variable;variable.printstaticint (); Variable.printstaticconstint (); return 0;}

The result of compiling in g++ is:

Error with VS compilation: "Error LNK2001: unresolved external symbol" Private:static int const testvariable::staticconstintvariable "([email protected]@@ 0HB) ", and the error message in case 2 is the same.

It is only defined in two ways, defined within a 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 without qualifier, this does not need to be discussed const int constintvariable;//Case 1 const constant static int staticintvariable;// Case 2 static variable <span style= "color: #ff0000;" >static Const int staticconstintvariable=3;//case 3 static constant integral type </span>static const float staticconstnotintvariable ;//condition 4 static very integral type}; int Testvariable::staticintvariable=1;int Main () {testvariable variable;variable.printstaticint (); Variable.printstaticconstint (); return 0;}
Outside the class definition:

#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 without qualifier, this does not need to be discussed const int constintvariable;//Case 1 const constant static int staticintvariable;// Condition 2 static variable statics const int staticconstintvariable;//condition 3 static constant integer type static const float staticconstnotintvariable;//condition 4 static very integral 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;}
Note that the Const keyword here also needs to be, otherwise this error will appear in g++: it will take it as a new variable, but this variable has the same variable name as the original const type, so an error occurs.



The Prompt in VS is: "Error C2373:" staticconstintvariable ": redefine; different type modifier".

Case 4:static Const-Modified non-integer data

Then there is the case 4, for static constant non-integer data, if initialized within 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;} void Printstaticconstnotintvariable () {cout<< "staticconstnotintvariable:" <<staticconstnotintvariable <<endl;} Private:int intvariable;//case 0 without qualifier, this does not need to be discussed const int constintvariable;//Case 1 const constant static int staticintvariable;// Condition 2 static variable statics const int staticconstintvariable;//case 3 static constant integral type <span style= "color: #ff0000;" >static Const FLOAT staticconstnotintvariable=4.0;//case 4 static very integral type </span>}; int testvariable::staticintvariable=1; const int Testvariable::staticconstintvariable=3;int Main () {testvariable variable;variable.printstaticint (); Variable.printstaticconstint (); variable.printstaticnotintvariable (); return 0;} 
There are no problems in g++:

In VS 2008, this error "Error C2864:" testvariable::staticconstnotintvariable ": only static constant integer data members can be initialized in the class ."

So it's best not to write this code.

The initialization of a variable is placed outside the class and in case 3:

#include <iostream>using namespace Std;class testvariable{public:testvariable (): constintvariable (0) {}void Printstaticint () {cout<< "staticintvariable:" &LT;&LT;STATICINTVARIABLE&LT;&LT;ENDL;} void Printstaticconstint () {cout<< "staticonstintvariable:" &LT;&LT;STATICCONSTINTVARIABLE&LT;&LT;ENDL;} void Printstaticconstnotintvariable () {cout<< "staticconstnotintvariable:" <<staticconstnotintvariable <<endl;} Private:int intvariable;//case 0 without qualifier, this does not need to be discussed const int constintvariable;//Case 1 const constant static int staticintvariable;// Condition 2 static variable statics const int staticconstintvariable;//condition 3 static constant integer type static const float staticconstnotintvariable;//condition 4 Static very full 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 (); return0;} 

This is the correct result for running in g++ and vs.


Analysis of member variables plus static or const keywords in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.