I've got an entity class, a base component class and a few base component derived ones. The entity class contains an array of base component pointers:
class CComponent
{
public:
static unsigned GetType() { return -1; }; //Base identifier
//...
};
class CPoint : CComponent
{
public:
static unsigned GetType() { return 0; }; //Point identifier
}
class CEntity
{
private:
CComponent* aComponents[3];
public:
// ...
//Getter by component ID here!
};
I would like to know how can I map a specific component type along with its integer identifier (for the CPoint component class, it would be 0) so I can easily cast it to the right type, in my Entity class.
Example: Suppose I've added a CPoint component to the entity component array (in position 0 of course), and I want to retrieve it as a CPoint (type-casting) by inputting the component integer identifier (in this case, 0). I want to avoid huge 'switch' cases as much as possible!
PS: I don't want to use loads of virtual functions in my base component class that match with properties in my derived ones. (I don't want to have a virtual SetPos function in my base component class, while it's place is in the CPoint class);
PS#2: As commented by 'etarion', I would like something like this:
dynamic_cast<get_type_for(0)*>(obj)
Of course, that's the mechanism I want to achieve, I don't know if it's possible or not.
Well I know of some ways to achieve the sort of thing you want :
typeof(some_type)
function which gives you the type to dynamically cast your type. See here.decltype(some_type)
typeid
that gives you a type_info class which implements operator==
my2c