I have different collections of Components, where each Component subclasses an abstract class IComponent.IComponent defines a pure virtual function virtual ID_TYPE getId() = 0;
, so each component need to create an implementation for it.
Consider each Component may only be added once, so its ID equals for all instances and a collection function
IComponent* ICollection::getComponent(const ID_TYPE);
I want to have an additional static method just like the one described above in order to retrieve the ID of a component without the need to create an instance (there might be no default constructor) in order to something like
collection1.getComponent(MyComponentA::getId())->something();
collection2.getComponent(MyComponentA::getId())->something();
I know there aren't such things as virtual static members, and a static method cannot be overridden. But might there be any other way to ensure each subclass of IComponent
defines such a static method?