how do i define a const inside a class under private
when i try to define say
class abc
{
private: //or public whatever
cont int a = 12; //i get 'illegal' errors
}
using vc6 9x
how do i define a const inside a class under private
when i try to define say
class abc
{
private: //or public whatever
cont int a = 12; //i get 'illegal' errors
}
using vc6 9x
class abc
{
abc() {a = 12}
private: //or public whatever
const int a;
}
I think you can do this when it is initialized in the constructor.
As the Troll mentioned, you can set constant members in the constructor like so:
class MyClass
{
public:
MyClass() : cvar(5)
{
}
private:
const int cvar;
};
If you're going to use a constant data member, however, you should declare it as static. That way all instances of an object only share that one constant rather than every instance having their own version and all of them being the same.
class MyClass
{
private:
static const int cvar;
};
Remember to define the static somewhere in your sources or you will get a linker error:
const int MyClass::cvar = 5;
Hope this helps
..or you could use an enum to guarantee that it will be evaluated at compile time and not have storage allocated (as MSVC 6.0 doesn't support initialisation of static consts from within class declarations) -
Code:class abc { enum {a =12}; };
zen