First, the static variable is initialized only once, whether in the class or in the function... there is such a function:
Void Foo () <br/>{< br/> static int A = 3; // initialize <br/> STD: cout <; <br/> A ++; <br/>}
Here, static int A = 3 is executed only once. Call Foo () twice in main and the result is 34. Change the above function
Void Foo () <br/>{< br/> static int A; <br/> A = 3; // not initialize <br/> STD :: cout <A; <br/> A ++; <br/>}
It is also called twice in Foo (). The result is 33.
Use non-const static class member variables in the class. Use the form of typename classname: variablename = value during initialization
For example:
Class myclass <br/>{< br/> Public: <br/> static int A; <br/> myclass () <br/>{< br/>}< br/>}; <br/> int myclass: A = 3; // here initialize <br/> int main () <br/>{< br/> cout <myclass: A; <br/> return 0; <br/>}
If you are using a static variable of the const type, initialize the variable in the class:
Class myclass <br/>{< br/> Public: <br/> const static int A = 3; // here initialize <br/> myclass () <br/>{< br/>}< br/> };
If non-const static variables are used in the template, You need to initialize them according to the specific type.
For example, int myclass <int >:: A = 4; if you call cout <myclass <double>: A, the compilation will fail.
Because the template is of a specific type, myclass <int> and myclass <double> are a specific type, and a static class member is initialized once in a specific class. This is easy to understand.