static_cast
is not the right tool for the job, unless you know that the pointer goes to an animatedGameObject
and not a gameObject
. What data structure are you using to store that information?
Determining the type of a derived object after a base pointer is the job of dynamic dispatch or dynamic_cast
. In your example, the call GameObjects[1]->draw()
should work with no cast because draw
should be a virtual function. Otherwise you can use dynamic_cast< animatedGameObject & >( * GameObjects[1] )
to assert that the object is an animatedGameObject, and throw a std::bad_cast
exception if it's not. (This would still require a virtual
function in class gameObject
, usually its destructor.)
But doing static_cast
to a polymorphic derived type is a code smell.
Also you ask whether std::vector
is a good data structure for this use case. It is, but not a vector of "naked" pointers. C++11 now provides "smart pointer" memory management classes which perform new
and delete
for you, rendering the actual operators all but obsolete. Look into std::unique_ptr
for this case.