How to initialize a static const member in C ++?

Is it possible to initialize the static value of const outside the constructor? Can it be initialized in the same place where participants' announcements are found?

class A { private: static const int a = 4; /*...*/ }; 
+43
c ++ static
Aug 20 '10 at 13:06
source share
5 answers

YES you can, but only for int types. If you want your static member to be any other type, you need to define it somewhere in the cpp file.

 class A{ private: static const int a = 4; // valid static const std::string t ; // can't be initialized here ... ... }; // in a cpp file where the static variable will exist const std::string A::t = "this way it works"; 

Also, note that this rule was removed in C ++ 11, now (using the compiler that provides this function) you can initialize what you want directly in the class member declaration.

+51
Aug 20 '10 at 13:12
source share

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; // definition outside class declaration 

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(); // initialize with static member function int C::j = C::i; // initialize with another static data member int C::k = cf(); // initialize with member function from an object int C::l = cj; // initialize with data member from an object int C::s = ca; // initialize with nonstatic data member int C::r = 1; // initialize with a constant value class Y : private C {} y; int C::m = Y::f(); int C::n = Y::r; int C::p = yr; // error int C::q = yf(); // error 

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:

 #include <iostream> using namespace std; struct X { static const int a = 76; }; const int X::a; int main() { cout << X::a << endl; } 

Tokens = 76 at the end of the declaration of the static data element a is a constant initializer.

+27
Aug 20 '10 at 13:11
source share

Just for completeness, I add static template member variables.

 template<class T> struct X{ static T x; }; template<class T> T X<T>::x = T(); int main(){ X<int> x; } 
+9
Aug 20 '10 at 13:52
source share

You cannot initialize static members inside constructors. Integral types that you can initialize inline when they are declared. Other static members must be defined (in the .cpp file):

 // .h class A{ private: static const int a = 4; static const foo bar; ... ... }; // .cpp const foo A::bar = ...; 
+3
Aug 20 '10 at 13:12
source share

If I remember correctly, you cannot define it inside a class. You must explicitly define it outside as indicated by pablo.

0
Aug 20 '10 at 13:11
source share



All Articles