Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use constructor initializer list to initialize non-static constant members.</p> <p>ISO C++03 says the following things about static data members.</p> <p><code>[class.static.data]</code></p> <p><code>9.4.2 Static data members</code></p> <blockquote> <p>1 A static data member is not part of the subobjects of a class. There is only one copy of a static data member shared by all the objects of the class.`</p> <p>2 The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a staticdata member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator. `</p> <p>If a static data member is of <strong>const integral</strong> or const enumeration type, its declaration in the class definition can specify a <strong>constant-initializer which shall be an integral constant expression</strong> (5.19). In that case, the member can appear in integral constant expressions. <strong>The member shall still be defined in a name-space scope if it is used in the program and the namespace scope definition shall not contain an initializer*</strong>.</p> </blockquote> <pre><code>class A { private: const int a=9; //incorrect static const int b = 10; //declaration (correct) static const double c = 1.3 //incorrect (Only const-static int members can be initialized like that) public: A(): a(9){} }; const int A::b; //definition of const-static int member </code></pre> <p>You can take the address of a static member if (and only if) <a href="http://www2.research.att.com/~bs/bs_faq2.html#in-class" rel="nofollow">it has an out-of-class definition:</a></p> <pre><code>class AE { // ... public: static const int c6 = 7; static const int c7 = 31; }; const int AE::c7; // definition int f() { const int* p1 = &amp;AE::c6; // error: c6 not an lvalue const int* p2 = &amp;AE::c7; // ok // ... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload