Dynamic down cast to abstract class (C++)
Solution 1
Cell class must have at least one virtual function to use dynamic_cast. Also, if Cell is your base class, it should have a virtual destructor.
You should make isAccessible a virtual function and override it in Obstacle to return false.
Solution 2
What you're doing is wrong. Generally you shouldn't need to cast to a sub type of a class in its base class. If you need it, it is likely a design error. In your case the code should look like this.
virtual bool Cell:: isAccessible()
{
return true;
}
bool Obstacle::isAccessible()
{
return false;
}
P.S. The cause of your error is that Cell
class does not have a virtual method and thus it does not show polymorphic behaviour.
tim_a
Languages: Fluent: Java, HTML, Javascript, CSS Learning: Various webtechnologies: jQuery, AngularJS, .Net framework (C#), Node.js Moderate: PHP, C, C++
Updated on June 04, 2022Comments
-
tim_a almost 2 years
I am trying to learn some object orientated programming aspect I know from java in C++. However I am having some difficulties in using
dynamic_cast
where I would useinstanceof
in Java.I have a base class
Cell
and a derived (abstract) classObstacle
. I have defined it like this:Obstacle : public Cell
andObstacle
contains a pure virtual destructor. Now in theCell
class I want to implement a methodbool Cell::isAccesible()
. I've implemented this as follows:bool Cell::isAccessible() { Obstacle *obs = dynamic_cast<Obstacle*>(this); if (obs != NULL) return false; return true; }
However I get the following error back:
"the operand of a runtime dynamic_cast must have a polymorphic class type".
What's wrong with the way I want to implement this? Any guidance is appreciated.
-
keyser about 10 yearsDoes this have anything to do with visual studio? Is it their error? (I'm asking because of the tag)
-
tim_a about 10 yearsI am coding this in visual studio that's all. I get the error while coding but also during compiling (C2683).
-
atoMerz about 10 yearsWhy are you casting Cell to obstacle in a Cell method?
-
YoungJohn about 10 yearsThis seems like a convoluted way to solve this problem. If
isAccessible
were a virtual method thenObstacle
could override it and simply return false and this would no longer be an issue. -
atoMerz about 10 yearsI don't know what you're trying to do, but the error is because your Cell class is not polymorphic i.e. it does not have any virtual methods. If you add a virtual method to Cell your code should compile.
-
David Rodríguez - dribeas about 10 yearsUsing
instanceof
in Java ordynamic_cast
to perform type switches in C++ is something you should avoid in the first place. You are creating cyclic dependencies as the base needs to know about the derived and vice versa.
-
-
tim_a about 10 yearsThanks. However apart from the fact I was trying to solve the problem the wrong way (by casting) why should Cell always have at least one virtual method in order to be able to use a dynamic_cast?
-
Neil Kirk about 10 years@tim_a dynamic_cast and virtual functions need some information to be embedded in the object, but this increases its size slightly, so C++ lets you choose whether to add the information.
-
mradul dubey over 2 yearsCan you please elaborate on why a virtual destructor is required ?
-
Neil Kirk over 2 years@mraduldubey stackoverflow.com/questions/461203/…