timmay3141
Programmer
I am writing a program where I will have a base class with an int that should stay constant, and derived classes that will have their own unique values. For example, it would be something like this:
class BaseClass
{
public:
const int num;
BaseClass() : num(0) {};
};
class DerivedClass : public BaseClass
{
DerivedClass() : num(1) {};
}
Basically, I want all of the derived classes to have their own unique values for num. Unfortunately, this won't compile without making num a non-const integer, because you must initialize constants in the initialization list of the base class. Is there any way I could get around this but leave num as a const?
class BaseClass
{
public:
const int num;
BaseClass() : num(0) {};
};
class DerivedClass : public BaseClass
{
DerivedClass() : num(1) {};
}
Basically, I want all of the derived classes to have their own unique values for num. Unfortunately, this won't compile without making num a non-const integer, because you must initialize constants in the initialization list of the base class. Is there any way I could get around this but leave num as a const?