const in class as a member variable?
-
Is it possible to declare member variable of class as a const, if yes,please give me 1 example how to initialize it and if not, why?
-
Is it possible to declare member variable of class as a const, if yes,please give me 1 example how to initialize it and if not, why?
Yes, it is possible. You have to initialize it in the constructor initialization list:
MyClass::MyClass() : myConstInt(5)
{
}You can also initialize it in the header file too, when declaring it:
class MyClass
{
const int myConstInt = 5;
}Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Is it possible to declare member variable of class as a const, if yes,please give me 1 example how to initialize it and if not, why?
sam_psycho wrote:
Is it possible to declare member variable of class as a const,
Yes.
sam_psycho wrote:
if yes,please give me 1 example how to initialize it
class A
{
static const int c = 299792458;
const int a;A():a(5){}
};:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Yes, it is possible. You have to initialize it in the constructor initialization list:
MyClass::MyClass() : myConstInt(5)
{
}You can also initialize it in the header file too, when declaring it:
class MyClass
{
const int myConstInt = 5;
}Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++This is working fine
MyClass::MyClass() : myConstInt(5)
{
}but it is not possible,
class MyClass
{
const int myConstInt = 5;
}compiler is giving error, So I guess only one way to initialize const in a class..
-
This is working fine
MyClass::MyClass() : myConstInt(5)
{
}but it is not possible,
class MyClass
{
const int myConstInt = 5;
}compiler is giving error, So I guess only one way to initialize const in a class..
Ooops, my bad. I don't use that notation very often. I thought it was ok but it is only valid if the member is static also.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
sam_psycho wrote:
Is it possible to declare member variable of class as a const,
Yes.
sam_psycho wrote:
if yes,please give me 1 example how to initialize it
class A
{
static const int c = 299792458;
const int a;A():a(5){}
};:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]from above discussion I have come to conclusion that compiler does not allow any type of initialization in class, so statement
static const int c = 299792458;
in your code also creating problem... :(
-
from above discussion I have come to conclusion that compiler does not allow any type of initialization in class, so statement
static const int c = 299792458;
in your code also creating problem... :(
You're wrong (I'm right, of course... :rolleyes: ). It looks like you missed the
static
modifier. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You're wrong (I'm right, of course... :rolleyes: ). It looks like you missed the
static
modifier. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]who is lying compiler or you?..but I should believe in compiler and it says that
error: c:\test.cpp 6: cannot initialize class member here
try this codeclass temp
{
static const int i=10;
};
void main()
{
} -
who is lying compiler or you?..but I should believe in compiler and it says that
error: c:\test.cpp 6: cannot initialize class member here
try this codeclass temp
{
static const int i=10;
};
void main()
{
}I never lie. Anyway maybe your compiler follows strictly the C++ stanbdard. (my compiler doesn't complain, see [^]). :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]