Reputation: 906
Curious if anyone has any ideas on this one; working on a game engine that implements a tree of GameObject's. Various specific game objects inherit from that type ( BouncingBall, StatScreen, or whatever).
class GameObject {
public:
vector<Component *> Components;
vector<GameObject *> Children;
GameObject *Parent;
};
So at runtime, the game will have a huge tree of these, all of different types. Unfortunately the objects in the Children vectors are all reported as "GameObject *" in the debugger making it really hard to tell what's what.
Best solution I've come up with is to store the type as a member variable on construction:
class GameObject {
public:
// ...
const char *Type;
// Saves current class name as Type.
template<class T>
void StoreTypeName( T *thisPointer ) {
this->Type = typeid( *thisPointer ).name();
}
};
But then in every object constructor I have to remember to call StoreTypeName:
class FancyBlinkingObject : public GameObject {
public:
FancyBlinkingObject() {
StoreTypeName( this );
}
};
Any other possible tricks to just do this automatically? Either on the debugger end (Visual Studio 2012) or in the code?
I thought of maybe some kind of central dynamic cast testing function:
if ( dynamic_cast<FancyBlinkingObject *>( basePointer ) ) {
return "FancyBlinkingObject";
}
if ( dynamic_cast<UglyBlueObject *>( basePointer ) ) {
return "UglyBlueObject";
}
But I can't think of a way to do it without me having to manually maintain the list of if's. Want something totally automatic if possible. :)
Upvotes: 2
Views: 2083
Reputation: 906
Per David Rodriguez's comment - making GameObject polymorphic (in my case by making the destructor virtual) changed its classification in the debugger and showed a vptr to the runtime type info, which did the trick.
Upvotes: 3