I'm currently working on an entity-component system in which I need to create unique IDs for different component types, preferably without storing any additional data in the component class (as that would require static virtual methods, which don't work, and so I would need to instance a new component every time I compare something). The approach I ended up going with was creating a templated method:
/*statically instances unique pointers for different
TComponents - each with their own templated instance*/
template<typename TComponent>
static const COMP_ID& GetTComponentID()
{
DERIVES_FROM_COMPONENT_ASSERT;
static COMP_ID ID = Component::NewID();
return ID;
}
and then have a static method in my component base class which returns a unique ID every time a new static COMP_ID is initialized in GetTCompomponentID():
//retrieves static data identifier
static const COMP_ID& NewID()
{
static COMP_ID currID = 0;
currID++;
return currID;
}
Using this, I can create unique, static IDs for each component derivative which I use without needing to store the data in the class itself as a virtual function (which can't be static, so every time a comparison is needed it would require a new instance)
Are there any underlying issues to using a system like this except the obvious extra memory utilized by the static method members?
-thanks