- #1
Mr Virtual
- 218
- 4
Homework Statement
I have my computer science exam today (in 2 hours). I want to know how to declare and initialize a const member in a class.
The Attempt at a Solution
I tried this:
_________________________________________________________________
class stu
{
const int a=10; // Error cannot initialize a data member here
};
--------------------------------------------------------------------------
Then I tried this:
_________________________________________________________________
class stu
{
const int a;
};
int stu::a=10; //Error : Multiple declaration of stu::a
--------------------------------------------------------------------------
Then this:
_________________________________________________________________
class stu
{
const int a;
stu() //constructor
{
a=10; //Error: Cannot modify a const member
}
};
--------------------------------------------------------------------------
Please help!
Thanks
Mr V