This is something that is possible in C# (see How to convert enum to type), but I wonder how I should implement it in C++11 ?
A possible code skeleton (which also shows my intended use) would be :
// classify.h (part of public API)
// definitions of classes A, B, C cannot be exposed, only a list of names
// (some names might even refer to the same type of object)
enum EAllowedTypes { eA, eB, eC };
class Entity;
bool classifyEntityAs(const Entity& ent, EAllowedTypes type);
_
// classify.cpp
#include "A.h" // defines class A (derived from a common base class)
#include "BC.h" // defines class BC (derived from a common base class)
template <typename T>
bool classify(const Entity &)
{
/* some implementation, with possible specializations */
}
// how to implement this kind of vector/map from enum(int) to existing classes
static const std::vector enumToType</*ClassType*/> =
{
/* eA */ class A,
/* eB */ class BC,
/* eC */ class BC
};
bool classifyEntityAs(cont Entity& ent, EAllowedTypes type)
{
return classify<enumToType[type]>(ent);
}
Aucun commentaire:
Enregistrer un commentaire