I have a Controller class which is designed to be inherited. It currently has a name() pure virtual method, like this:
class Controller {
public:
virtual const std::string name() const = 0;
}
The name is really a class-property, rather than an instance property. It shouldn't change between instances of a subclass, so really, virtual isn't the best thing to do here. Anyway, I've now hit a problem where I need the name at compile-time, in a template function. Obviously T::name() doesn't work, and because the subclasses won't be default constructible, I can't even hack around it with T()::name().
So something needs to change, but I can't figure out the best way to do this. I need to be able to access name in the following situations:
- At compile-time, when I have a subclass type (e.g.
T::name()) - At runtime, when I have an instance of a subclass (e.g.
instance->name()) - At runtime, when I have a
Controller*(e.g. pointer to the base class)
name() doesn't have to be method, it can be a public attribute, or something else. But no-matter what I try I can only seem to satisfy 2 of the above 3 requirements.
Can anyone suggest any good approaches for solving this?