#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 ?
~
const means that you're not changing the value after it has been initialised. static inside a function means the variable will exist before and after the function has executed. static outside of a function means that the scope of the symbol marked static is limited to that . c file and cannot be seen outside of it.
A static keyword is been used to declare a variable or a method as static. A const keyword is been used to assign a constant or a fixed value to a variable. In JavaScript, the static keyword is used with methods and classes too. In JavaScript, the const keyword is used with arrays and objects too.
Static variables are common across all instances of a type. constant variables are specific to each individual instance of a type but their values are known and fixed at compile time and it cannot be changed at runtime. unlike constants, static variable values can be changed at runtime.
A constant is a name given to the variable whose values can't be altered or changed. A constant is very similar to variables in the C programming language, but it can hold only a single variable during the execution of a program.
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
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With