class - C++: Deriving with variables. Something is wrong with a small section of code -
classes dog
, cat
derive animal
, in turn derives creature
. pdog
, pcat
, panimal
, , pcreature
pointers class. animal
, creature
both abstract classes.
what wrong code?
panimal = new dog(); pdog = panimal;
you can convert up hierarchy (i.e., derived base) implicitly.
converting down hierarchy (from base derived) has done explicitly. in cases, want use dynamic_cast
conversion succeed if correct:
animal *panimal = new dog(); dog *pdog = dynamic_cast<dog *>(panimal);
note classes need contain @ least 1 virtual function work (but if don't have virtual functions, hierarchy doesn't make sense @ all).
Comments
Post a Comment