i have 2 class: hObject, Drawer. Drawer inherit from hObject.
with this code i retrive a particular object by id:
hObject * foundObj = hManager::getInstance()->getObject("drawer_id");
now i want to correctly cast foundObj from hObject to Drawer. with:
vector<int> points = ((DrawerWidget *)hObject)->getPoints();
i recive
expected primary-expression before ‘)’ token
and what about dynamic cast?
consideration: my skills has been halved till i began c++
Two problems:
hObject(the type name) when you meantfoundObj(the object name)Fixing that, and also using a less dangerous C++-style cast, the following should work:
If the base class is polymorphic (that is, has at least one virtual function), then that would be safer still – you can check at runtime that the cast is valid:
although if you find yourself doing that, I would have a think about the design: it might be more appropriate to redesign the base class to support what you want to do via virtual functions, or to ensure at compile time that the conversion is valid.