data members can be initialized within a class
// Int maxnumber1 = 5;
// (2) Error c2864: 'mytest: maxnumber2': only static const integral data members can be initialized within a class
// Const int maxnumber2 = 5;
// (3) Error c2864: 'mytest: maxnumber3': only static const integral data members can be initialized within a class
// Static int maxnumber3 = 5;
// (4) OK
Static const int maxnumber4 = 5;
Static const char cconst4 = 'B ';
// (5) Error c2864: 'mytest: dconst4': only
/** * */#includeusing namespacestd;nbsp;classmytest{//(1) Error C2864: ' Mytest::maxnumber1 ': only static const integral data, can be initialized within a class /c8>//int MaxNumber1 = 5;nbsp; //(2) Error C2864: ' Mytest::maxnumber2 ': only static const integral data, can be initialized within a class /c0>//const int MaxNumber2 = 5;nbsp; //(3) Error C2864: ' Mytest::maxnumber3 ': only static const integral data, can be initialized within a class /c5>//static int MaxNumber3 = 5;nbsp; //
Const member variable InitializationConst member variables can only be initialized in the class constructor. The following compilation error is reported:
// Const. h # include
Print () {std: cout
Const int cs ;};// Const. cpp # include "const. h" cconst: cconst () {}int main (int argc, char *
Argv []) {cconst * a = new cconst (); a-> print (); return 0 ;}
Error c2758: 'cs ': must be initialized in constructor
Base/member initializer list
Change cons
Initialization of const member variablesConst member variables can only be initialized in the constructor of the class, and the following compile an error
Const.h#include
Print () {std::cout
const INT CS;Const.cpp#include "Const.h" Cconst::cconst () {}int main (int argc, char*
Argv[]) {cconst* a = new Cconst (); A->print (); return 0;}
Compile error Error c2758: ' CS ': Must be initialized in constructor
Base/member initializer list
Modify the
C ++ Primer:
// Constref. cpp: defines console applicationsProgram.//
# Include "stdafx. H"
Class constref{Public:Constref (int ii );
Int I;Const int ci;Int Ri;};
Constref: constref (int ii){I = II;Ci = II;Ri = I;}
// Constref: constref (int ii): I (ii), CI (I), RI (ii ){}
Int _ tmain (INT argc, _ tchar * argv []){Constref B (5 );
Printf ("% d, % d, % d", B. I, B. CI, B. RI );Return 0;}
In this case, there is an error in writing. Error
Initialization problems with class member variables in C + + after the static or const keyword. Define the following variables in a simple C + + class:#include After the qualifier is added 4, the following four cases are analyzed separately.Condition 1:const ModificationCompile 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
classConst intA::m_nstaticconst_3 = A;//out-of-class initialization static constantsA::a (): M_n3 ( A), M_nconst_2 ( One), M_nconst_3 ( A), M_nconst_4 ( -)//,m_dstaticconst_2 (11.0)//, M_nstaticconst_3 (a)//M_nstatic_3 (13)//Not a non-static data member or base class for class "a"{Errors 4 Error C2758: "a::m_nconst_1": Must be initialized in constructor base/member initializer listM_N2 = One;//m_nconst_3 = 12; Expression must be a modifiable left -ha
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.