class Component {
// Code here
};
class TransformComponent : public Component {
// Code here
};
class Entity:
public:
Component components[25];
TransformComponent *getTransform() {
for(int i = 0; i < 25; i++) {
if(typeid(components[i]) == typeid(TransformComponent())) {return *(components + i);}
}
}
};
I have an array of components, and inside could be any child class of "Component", like "TransformComponent". The thing is, when compiling, the computer thinks that the components array is only populated with "Component" objects. The function is supposed to return a "TransformComponent", and the compiler sees that as an error, even though the element in the array I am returning is a TransformComponent. Is there any solution to this (preferably simple)?