5
#include<iostream>
using namespace std;
class A
{
        private:
                const  int a=9;
        public:
                void display()
                {
                    cout<<a;
                }
};
int main()
{
        A a;
        a.display();
        return 0;
}

Why does initialization const int a=9 is not permitted. But where as if i write constant static int a=9 compiler does not show any error. What is the meaning of writing const static int a=9? when should i write like this ?
~

Jagan
  • 4,649
  • 19
  • 60
  • 70

3 Answers3

6

Use constructor initializer list to initialize non-static constant members.

ISO C++03 says the following things about static data members.

[class.static.data]

9.4.2 Static data members

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.`

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. `

If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions. 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*.

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

You can take the address of a static member if (and only if) it has an out-of-class definition:

class AE {
    // ...
public:
    static const int c6 = 7;
    static const int c7 = 31;
};

const int AE::c7;   // definition

int f()
{
    const int* p1 = &AE::c6;    // error: c6 not an lvalue
    const int* p2 = &AE::c7;    // ok
    // ...
}
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • With out writing Const int A::b i could compile this code successfully !!!!! why ? – Jagan Sep 25 '10 at 04:23
  • If only i don't initailze static const int b with some value it is giving error(if i don't write definition) ? why ? – Jagan Sep 25 '10 at 04:27
  • 1
    Successful compilation has nothing to do with the correctness of your code ->`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` – Prasoon Saurav Sep 25 '10 at 04:50
  • @Jagan : You are required to define the `const static` member even after you have declared it inside the class and specified an initializer(const expression) because the Standard says so. See the part in `bold`. – Prasoon Saurav Sep 25 '10 at 09:05
  • @Prasoon Saurav: 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. please follow this URL http://www.devx.com/tips/Tip/5602 .see the explanation. I am confused whether it is merely a declaration or both declaration and definition. – Jagan Sep 25 '10 at 09:26
  • 1
    @Jagan : `static const int MAX = 512; //definition` I don't agree with the article. It is just the declaration for `MAX` (with the initializer specified) not its definition. Also read the comments by Untruncated below that article. Check out the edits. – Prasoon Saurav Sep 25 '10 at 09:43
  • I am surprised even Stroustrup seems to assume in some situations the separate definition is not needed: his interpretation in the linked text is without the separate definition in legal, but is not l-value. The whole world just shattered :) either the C++ Standard or C++ Father seems to be wrong in this case. ;) – Suma Feb 03 '11 at 20:37
2

To initialize the const object, you'll need to do it within the constructor because it is a per object instantiation of an int. On the other hand, the static const variable is different in that it is shared amongst all the objects of that type. That means its allocated in the data segment, in a separate location to the variables within the object. It must be defined outside the constructor because it's only going to be set once, ever and not once per object.

edit: fixed "on the stack" as per casablanca's suggestion

wheaties
  • 35,646
  • 15
  • 94
  • 131
  • +1 though "sits on the stack" isn't correct: static variables are usually allocated in the data segment, not on the stack. – casablanca Sep 25 '10 at 04:00
0

static is a keyword used for instance-less variables/methods of a class. Therefore stating:

static int a=9;

means that a lives for the life of the application. The const keyword means a value cannot change. Therefore const static int a=9; declares a variable that is both static (lives for the application) and does not change value.

The reason you cannot state:

const int a=9;

Is because non-static members cannot be initialized like this, and need to be initialized in the constructor. Since you cannot change constant variables outside there declaration, constant non-static members become impossible.

EDIT: Also note that a const non-static member is unnecessary. Because you member will never change value, making it static to get around errors will make no difference to your code, besides the fact that you can access it with A::a, instead of needing an instantiation of A.

Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55