Having trouble downcasting in c++ -


i iterating through list of animal objects (which contains 3 or 4 different types of objects subclassing animal):

foreach (animal entry, animallist) {      switch(entry.animaltype)     {     case animal::tiger:         qdebug() << static_cast<tiger>(entry).tigerstring;         break;     } } 

this gives me following error:

no matching function call 'tiger::tiger(animal&)'

so tried:

static_cast<tiger*>(entry).tigerstring;

which gives me following error:

invalid static_cast type 'animal' type 'tiger*'

so decided change entry pointer type so:

foreach (animal* entry, animallist) etc....

and following error:

cannot convert 'const value_type {aka const animal}' 'animal*' in initialization

am missing here? absolutely need tigerstring string specific subclass tiger.

what should doing?

update please see following (the code has been stripped cleanliness):

std::list<animal*> animallist;  tiger *mytiger = new tiger(); mytiger->animaltype= animal::tiger; mytiger->tigerstring= "i tiger";  animallist.push_back(mytiger, animallist);  foreach (animal* entry, animallist) {     tiger* tiger = dynamic_cast <tiger*> (entry);      if (tiger)     {         // tiger     }     else     {         // not tiger      } } 

i following error @ first line in foreach loop:

cannot dynamic_cast 'animal' (of type 'class animal*') type 'class tiger*' (source type not polymorphic)

your function takes animal by-value, rather by-reference or pointer. i'm guessing have polymorphic hierarchy designed animal abc , tiger concrete derived class.

if so, code certianly wrong. taking animal value, slice object, , you're left animal. tigerness gone.

when did

static_cast<tiger*>(entry).tigerstring; 

you trying cast non-pointer pointer. never work. change function take animal reference (or pointer).

you trying use wrong cast. should using dynamic_cast, not static_cast here, when going base derived polymorphic object.

do this:

foreach (animal* animal) {   tiger* tiger = dynamic_cast <tiger*> (animal);   if (tiger)   {     // tiger   }   else   {     // not tiger   } } 

finally, comments discovered not using polymorphic types. polymorphic class 1 has @ least 1 virtual member function in base class. 1 of virtual member functions should destructor in cases1:

class animal { public:   virtual ~animal(){}; }; 

...even if trivial (no implementation). in many cases might only virtual member function, , that's fine.


1 "one of virtual member functions should destructor in cases" : needed when delete instantiation of derived class via pointer base class, with:

class animal { public:   virtual ~animal() {} };  class tiger : public animal { };  int main() {   animal* = new tiger;   delete a; // without virtual destructor, evoke undefined behavior } 

rule of thumb: when in doubt, add virtual destructor polymorphic base classes.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -