Reputation: 38052
In C++, pure virtual functions provide the functionality of an interface. That is, any subclasses must implement all pure-virtual functions in the base class:
class myClass {
virtual bool implementme() = 0; // MUST be implemented
};
class mySubClass : public myClass {
bool implementme() {} // REQUIRED
};
Is there a similar mechanism for nested (enum
) class
es? That is, I'm looking for something like
class myClass {
virtual enum class myEnum = 0; // MUST be implemented
};
class mySubClass : public myClass {
enum class myEnum {}; // REQUIRED
};
Upvotes: 2
Views: 279
Reputation: 44201
Since you say that the implementer is not part of your code base (thus not producing a compile error), I must assume you are writing a library, and that the code which uses this enum is in the consumer of the library.
I would recommend that you use CRTP as follows:
class myClass {
};
template<typename T> class myClassImpl : public myClass {
static_assert(std::is_enum<typename T::myEnum>::value, "Subclasses of myClassImpl must provide the myEnum enum class");
};
class mySubClass : public myClassImpl<mySubClass> {
enum class myEnum {};
};
Upvotes: 1
Reputation: 28178
This wouldn't make a whole lot of sense. Someone could only have visibility to only the base class (not the derived class) and get back a myEnum
from the return of some virtual function, where myEnum
is an incomplete type. There is no mechanism for virtual types of any kind, including enums. Do you really want a virtual table looking up your type anyway?
Upvotes: 0