I have a type definition of typedef vector<Object*> ObjList;
I also have a function vector<BigObject*>* ObjectBox::getBigObjectList();
. BigObject
is inhertied from Object
What I wanted to do is to get a vector<BigObject*>*
from getBigObjectList()
and convert it to vector<Object*>*
, which is an upward casting, and this type is defined as ObjList
so I basically wanted to to conver that into a ObjList
type
I tried two ways, the first is
ObjList *normalObjectList = (ObjList*) box->getBigObjectList();
This compiles and I read from this article (When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?) which says C-style casting is rarely desireable since it can be develped into a reinterpret-cast
Then I try to use static_cast
, but I got an error saying Invalid type conversion
ObjList *normalObjectList = static_cast<ObjList*> (box->ClipObjectInRect());
Neither will this work
ObjList *normalObjectList = static_cast<vector<Object*>*> (box->ClipObjectInRect());
Why isn't this working? Is this because static_cast
can only be used to cast a direct class (like Object
itself) instead of a nested one (I'm just totally guessing here)? What should I do in this case?