I am curious what constructs or language functions available in both current C ++ and C ++ 11 can be used to infer an object type. Example:
class Base { }; class DerivA : public Base { }; class DerivB : public Base { }; void foo(Base* obj) {
This is a simplification. It seems that instead of having a way to identify the type, the best solution is to overload the function for two derived types and eliminate the base class.
My real use case is where one class is not interested in the exact type of the object (i.e. just needs a Base implementation), and the other class needs to know exactly which Base implementation of the first class is used.
This happens in a component-based entity system. The base will be EntityState , and its derived types: StandingState , DeadState , etc. The Entity class is the one that needs only a generic EntityState , and the EntityRepresentation class must know exactly what state the subject must decide whether to draw a "standing" animation or a "dead" animation or something else.
Edit: Of course, if possible, I would like to implement the game in such a way that even the entity representation does not need to know the type of state of the object. If there is a way to do this, I would use it. :) I will consider the visitor template.
source share