About static and const value in Class-define, let me record my test code and analysis the reason.
#include<iostream>
2 class test
3 {
4 public:
5 static const int m_const=1;//right
6 //static const char m_const[]="abcd";//error
7 static const char m_const_char[];
8 static int m_static;
9 };
10 //const int test::m_const=1;//right
11 const char test::m_const_char[]="abcd";//right
12 int test::m_static=2;
13 int main()
14 {
15 //const int test::m_const=1;//error
16 //int test::m_static=2;//error
17 std::cout<<test::m_const<<test::m_static<<std::endl;
18 return 0;
19 }The analysis is as below:
a) "static" identifier is to define the storage information, it must be declared in Class-Define-Area, and initialized in other places (shown in c) to allocate memory for the value.
while "const" is to define the type information, "const int " and "int" are different types.
So, when initializing, the identifier "static" must be omitted, while "const" not.(line 10,11,12)
b) Identifier "static" can only be used for one time for one value name. So if you change the line 10 to "static const int test::m_const=1;", error.
c) static value can only be initialized in 3 places.
P1: test.cpp
P2: out of the class and before the main(); Attention: cannnot be initialized in main(), for the reason: static value must be allocated memory before the program-running.
P3: in the Class define area(e.g. line 5); But it only fit some situation, e.g. int/char, but not for int[]/char[](e.g. line 6);