[Link to this article]
Http://www.cnblogs.com/hellogiser/p/static-const.html
[Analysis]
Const data members must be initialized in the constructor initialization list;
Static data members must be initialized globally and cannot have static delimiters, such as int class: size = 100;
For static const and const static members, only the int type can be initialized internally. Any type can be initialized outside the class, but cannot be initialized in the constructor list, you need to add the const keyword. For example, const int class: size = 100;
[Code]
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
/* Version: 1.0 Author: hellogiser Blog: http://www.cnblogs.com/hellogiser Date: 2014/9/20 */
# Include "stdafx. H" # Include "iostream" Using namespace STD;
Class myclass { Public: Myclass (INT size): size1 (size) {
} ~ Myclass () {
} Const int size1; // member initializer list Static int size2; // outside class
Static const int size3 = 150; // only int can be initialized inside class Static const int size4; };
Int myclass: size2 = 100; // static Const int myclass: size4 = 200; // static const
Void test_class_const_static_variable () { Myclass OBJ (50 ); Cout <obj. size1 <Endl; Cout <obj. size2 <Endl; Cout <obj. size3 <Endl; Cout <obj. size4 <Endl; }
Int main () { Test_class_const_static_variable (); Return 0; } /* 50 100 150 200 */ |
[Reference]
Http://blog.csdn.net/gukesdo/article/details/7435805
Static-const class member variables