const and static specifiers in c++
#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 p开发者_JS百科ermitted. 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 ?
~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
// ...
}
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
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.
精彩评论