Static Data Elements (C ++ only)
Declaring a static data member in a list of class members is not a definition. You must define a static member outside the class declaration in the namespace scope. For example:
class X { public: static int i; }; int X::i = 0;
Once you define a static data member, it exists even if there are no objects in the static data class. In the above example, objects of class X do not exist, although the static data element X :: I am defined.
Static members of the class data in the namespace have an external relationship. The initializer for the static data member enters the scope of the class declaring the member.
The static data element can be of any type, with the exception of void or void qualified with const or volatile. You cannot declare a static data member as mutable.
A program can have only one definition of a static member. Classes of a class, classes contained in unnamed classes, and local classes cannot contain static data.
Static data members and their initializers can access other static private and protected members of their class. The following example shows how you can initialize static members using other static members, although these members are private:
class C { static int i; static int j; static int k; static int l; static int m; static int n; static int p; static int q; static int r; static int s; static int f() { return 0; } int a; public: C() { a = 0; } }; C c; int C::i = C::f();
The initializations of C :: p and C :: q lead to errors, because y is an object of the class, which is obtained privately from C, and its members are not accessible to members of C.
If the static data member is of type const const or const, you can specify the constant initializer in the declaration of the static data member. This constant initializer must be an integral constant expression. Note that a constant initializer is not a definition. You should still define a static member in the encompassing namespace. The following example demonstrates this:
Tokens = 76 at the end of the declaration of the static data element a is a constant initializer.