Pergunta

Is it possible to somehow use a class name as key in a container?

I want to store objects of classes inherited from the same base.

struct storage {};

struct storagetransform : public storage
{
    vec3 position, rotation;
};

struct storageform : public storage
{
    unsigned int vertex, texture;
};

I have a nested map. The ... should be filled with the specific class name like storagetransform or storageform as a type or string or whatever.

unordered_map<..., unordered_map<int, storage*> > list;

I want to access the object of the container like the following.

list[storagetransform](1337);

It this possible somehow? Moreover is there a better approach for this problem?

Foi útil?

Solução

If you do accept to access your list like this:

list[ type_index ( typeid(classname) ) ]

you can use the std::type_index to map your class to a indexable (usable in std::map and the like) object and you can declare your std::map as follows

std::map< std::type_index , ... > list;

As far as I remember this needs a compiler that complies to the new C++ standard.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top