Const member variable Initialization
Const member variables can only be initialized in the class constructor. The following compilation error is reported:
// Const. h # include <iostream> class cconst {public: cconst (); void
Print () {std: cout <"----" <cs <std: endl;} private:
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 const. cpp:
# Include "const. h" cconst: cconst () {cs = 0;} int main (int argc, char *
Argv []) {cconst * a = new cconst (); a-> print (); return 0 ;}
Still error:
Error c2758: 'cs ': must be initialized in constructor base/member
Initializer list
After it is changed to the following format, it is normal:
# Include "const. h" cconst: cconst (): cs (0) {// cs = 0;} int main (int argc,
Char * argv []) {cconst * a = new cconst (); a-> print (); return 0 ;}