[1] "in situ" statement
C ++ 98, the support the use of the equal sign in the class declaration when "=" initialize static class member variables, such statements manner we call "local" statement.
However, C ++ 98 members must meet the requirements of a static constant, but also must be an integer type or enumeration, rather than a static member variable is initialized to be performed in the constructor.
The following example:
. 1 class the Init 2 { . 3 public : . 4 the Init (): A ( 0 ) . 5 {} . 6 the Init ( int D): A (D) . 7 {} . 8 Private : . 9 int A; 10 const static int B = 0 ; . 11 int C = . 1 ; // member, not compile 12 is static int D = 0 ; // members, not compile 13 is static const Double E = 1.3 ; // non-integer or enumeration, not compile 14 static const char * const F = " E " ; // non-integer or enumeration, not compile 15 };
As each case not compile.
[2] The difference in C ++ 11
In C ++ 11 is allowed to use the = or braces {} in situ non-static member variables are initialized. The following example:
1 struct C 2 { 3 C(int i, int j) :c(i), d(j) 4 {}; 5 6 int c; 7 int d; 8 }; 9 10 struct init 11 { 12 int a = 1; 13 string b{ "hello" }; 14 C c{ 1, 3 }; 15 };
We can see from the line 12, 13, with an equal sign or curly braces-place initialization.
So after use Notes :
[1] effect initialization list always takes precedence over the in-place initialization. The following example:
1 #include <iostream> 2 using namespace std; 3 4 struct Test 5 { 6 Test() {}; 7 Test(int i) : c(i) 8 {}; 9 10 int c = 2; 11 }; 12 13 int main() 14 { 15 Test c1; 16 Test c2(500); 17 18 cout << c1.c << endl; // 2 19 cout << c2.c << endl; // 500 20 }
[2] For very static member variable amount, C ++ 11 consistent with C ++ 98:
In still yet to define its position outside the header file, which will ensure that compile time, the definition of a static member of the class last only exist in a target file.
The following example:
1 // correct situation: 2 struct S . 3 { . 4 S () {}; . 5 S ( int I): S (I) {}; . 6 . 7 int S = 2 ; . 8 static int P; . 9 }; 10 . 11 int :: P = S . 3 ; 12 is 13 is // error conditions: 14 struct C 15 { 16 C () {}; . 17 C ( int I): C (I) {}; 18 is . 19 int c = 2; 20 static int d = 3; //报错 Non-const static data member must be initialized out of line 21 };
[3] Compared with the traditional initialization list, in the class declaration of non-static member variables in situ list initialization can reduce the workload of programmers.
Of course, we only have multiple constructors, and there are multiple variables when multiple members can see the convenience of new ways. The following example:
. 1 #include < String > 2 the using namespace STD; . 3 . 4 class Mem . 5 { . 6 public : . 7 Mem ( int I): m (I) . 8 {} . 9 10 Private : . 11 int m; 12 is }; 13 is 14 class Group 15 { 16 public : . 17 Group () {} // here does not require initialization data, mem, name of member 18 is Group (int A): Data (A) {} // here does not require initialization mem, name the members . 19 Group (Mem m): MEM (m) {} // here does not require initialization data, name of member 20 is Group ( int A, Mem m, String n-): Data (A), MEM (m), name (n-) 21 is {} 22 is 23 is Private : 24 int Data = . 1 ; 25 Mem MEM { 0 }; 26 is String name { " Group " }; 27 };
good good study, day day up.
Select the cycle order summary