I have class Base and classes Derived_1, Derived_2 ...
I need derived classes to have an id. Those ids are used for further lookups etc, and thus need to be consecutive (no just some random numbers). Because derived classes are created by user, id can not be member of Derived_N. So I came up with DerivedType class.
class DerivedType
{
static unsigned id;
unsigned m_id;
public:
DerivedType() : m_id(id++) { }
}
Now I want to create a mapping between Derived_N and DerivedType.
Whenever Derived_N is created, this mapping looks if DerivedType for particular Derived_N already exist and returns it, otherwise create new and stores in the map.
Actual question:
Is there any way to use std::map with data type as key in the map?
I am not afraid of any template-metaprogram solution.
Or is there elegant way how to achieve my goal?
edit Date type -> Data type, I mean like ClassType, I am sorry :)
I want to use it like:
Derived_5 d;
DerivedType dt = getType(d); //Derived_5 is looked up in map, returning particular DerivedType
dt.getId();
every instance of Derived_N (with same 'N') should have the same id throu DerivedType
EDIT2 - MY ANSWER I found better solution for my problem... It is like this:
atomic_counter s_nextEventClassID;
typedef int cid_t;
template<class EventClass>
class EventClassID
{
public:
static cid_t getID()
{
static cid_t classID = EventClassID::next();
return classID;
}
static cid_t next() { return ++s_nextEventClassID; }
};
since my question was how to use datatype in a map, I will mark some of yours answers, thank you