yakky
asked on
How do I initialise a const?
Ok so Im being really dumb,
My problem is
How do I initialise this constant?
If I initialise it in the header it throws an error.
If I initialise it as shown below it thows an error.
So what is the correct way of initialising it?
Thanks in advance Yakky
class demo
{
public:
const int myint;
demo();
~demo();
};
demo::demo()
{
myint = 5;
}
My problem is
How do I initialise this constant?
If I initialise it in the header it throws an error.
If I initialise it as shown below it thows an error.
So what is the correct way of initialising it?
Thanks in advance Yakky
class demo
{
public:
const int myint;
demo();
~demo();
};
demo::demo()
{
myint = 5;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Excuse me. I'm beta testing the new version of EE and a bug just occured. I will be back to finish this in a few minutes. As I said, there are a bunch of issues in this question.
Hi yakky
Use this :
demo::demo() : myint (5)
{
}
It is just like calling constructors from baseclasses. In fact, you are calling the construct function from the integer.
Use this :
demo::demo() : myint (5)
{
}
It is just like calling constructors from baseclasses. In fact, you are calling the construct function from the integer.
I'm back.
Now, what this does for you is to allow you to define a constant data member inside an object of a class. This means that each object fo the class has its own copy of this constant data member and therefor may have different values for tor this data member, for example if you did
class Demo
{
const int MyInt;
public:
Demo(int i) :
MyInt(i)
{
};
};
Demo A(1);
Demo B(2);
A would have MyInt set to 1 and B woudl have it set to 2. Note that MyInt would still be constant, that is, it could not "legally" be changed in either A or B after initialization.
But I suspect that this is not what you want. I suspect you want one constant that is shared among the objects of the class. (i.e. one constant that is the same for all objects in the class.) For that you need to use a static int or an enum. Is that what you want?
Now, what this does for you is to allow you to define a constant data member inside an object of a class. This means that each object fo the class has its own copy of this constant data member and therefor may have different values for tor this data member, for example if you did
class Demo
{
const int MyInt;
public:
Demo(int i) :
MyInt(i)
{
};
};
Demo A(1);
Demo B(2);
A would have MyInt set to 1 and B woudl have it set to 2. Note that MyInt would still be constant, that is, it could not "legally" be changed in either A or B after initialization.
But I suspect that this is not what you want. I suspect you want one constant that is shared among the objects of the class. (i.e. one constant that is the same for all objects in the class.) For that you need to use a static int or an enum. Is that what you want?
ASKER
Yes I wanted every instance of the class to have the same number how do I do that?
Hi yakky,
Like nietod mentioned, use a static.
class demo
{
public:
static int myint;
demo();
~demo();
};
int demo::myint = 5;
demo::demo()
{
}
demo::~demo()
{
}
Like nietod mentioned, use a static.
class demo
{
public:
static int myint;
demo();
~demo();
};
int demo::myint = 5;
demo::demo()
{
}
demo::~demo()
{
}
According to the standard, you can declare a static constant in the class and initialze the static constant soemwhere (The initialization must occur outside the scope of the class and may occur only one time. so it would not usually be placed inside the class's .h file.) Like
// in a .h file. (or in a .cpp file.)
class Demo
{
static const int MyConst;
public:
};
// Not in a .h file.
const int Demo::MyConst = 5;
However, not all compilers support this yet--although it is standard C++. In addition, having to initialize the static only one time is sometimes a pain (because it can't go in the .h file.) So another aproach is to use an enum nested in the class instead, like
class Demo
{
enum { MyConst = 5 };
};
The advantage is that the constant value may be specified righ in the class declaration in the .h file. the dissadvantage is that you can't specify the data type used to store the constant. i.e you can't make it an unsigned int, for example. And this approach only works with numerical constants. The other approach could be used for any constant, like a pointer, or a class.
// in a .h file. (or in a .cpp file.)
class Demo
{
static const int MyConst;
public:
};
// Not in a .h file.
const int Demo::MyConst = 5;
However, not all compilers support this yet--although it is standard C++. In addition, having to initialize the static only one time is sometimes a pain (because it can't go in the .h file.) So another aproach is to use an enum nested in the class instead, like
class Demo
{
enum { MyConst = 5 };
};
The advantage is that the constant value may be specified righ in the class declaration in the .h file. the dissadvantage is that you can't specify the data type used to store the constant. i.e you can't make it an unsigned int, for example. And this approach only works with numerical constants. The other approach could be used for any constant, like a pointer, or a class.
toronado, that is close, but it is not constant. So there is one value that is shred among the objects, but that value may be changed. You just need to add the "const" keyword.
ASKER
Thankyou very much
Yakky
Yakky
time of declaration.
You can say
const int myint = 10;