I have this basic setup:
enum{
BASE,
PRIMITIVE,
...
};
class IUnknown{
public:
bool inline is(int type){return inv_type == type;}
private:
enum {inv_type = BASE};
};
class Primitive: public IUnkown{
private:
enum {inv_type = PRIMITIVE};
};
My problem is that I would want to be able to call is on a Primitive instance and have it return true when type is equal to the value in the enum I have declared in the Primitive class.
The only solution I have found is to declare the ‘is‘ function as virtual and have a copy in every subclass, but I wondered if it would be possible to somehow redefine the enum and have the is function in IUnkown take the value from there
You could have your IUnknown class define a protected constructor (which would then have to be called from each derived class). It would take one of the enum values and store it. The stored value would then be compared against in the is() method.
If you don’t like this, and prefer to add a virtual is() method to IUnknown, but don’t want to have to define it in every derived class, you could do this: