Click to See Complete Forum and Search --> : How to declare and use const member variable in class


RRam_k
July 23rd, 2002, 02:45 AM
Hi NG,
I want to use a const member variable in a class like
class abc{
private :
const int x;

};

How can i Initialise this into some value.
I am using MS vc++ 6.0.
Regards\Raghuram.k

Graham
July 23rd, 2002, 03:18 AM
You have to set it in a constructor initialiser list:

class abc
{
public:
abc() : x(0) {}
abc(const abc& other) : x(other.x) {}

private:
const int x;
};