Advertisement

the operand of a runtime dynamic_cast must have a polymorphic class type error

Started by November 21, 2024 01:31 PM
4 comments, last by Calin 1 month ago
cladire* Cladir = dynamic_cast<cladire*>(Locations[i]);

Why am I getting this error? What's wrong with Locations[i] ? I am using inheritance already. The syntax I'm using is this:

class Location
{
public: 

and

class cladire : public Location
{
public:

Am I missing something?

My project`s facebook page is “DreamLand Page”

Since you skipped the definition of the classes, I'm assuming that “Location” does not have any virtual function? In order for dynamic_cast to work, Location must define at least one virtual method. That's what the compiler is trying to tell you.

In general, if you intend to store and delete children of “Location” via a pase-ptr, similar to this:

Location* location = new cladire();
// later
delete location;

Then you need to, at least, give Location a virtual destructor, otherwise you have a memory leak (the above code would not call the destructor.

class Location
{
public:
	virtual ~Location(void) = default;
};

This would also fix the error you are getting, without the need to add any other virtual methods.

Note also that your example reeks of a code-smell, you should not need to downcast to a specific location like that. Location should have data-members/virtual functions to allow you to describe any specific location, and write any algorithm in terms of just the “Location” type. Downcasting is often a sigh of bad design.

Advertisement

Juliean said:
That's what the compiler is trying to tell you.

Yup, that's literally the error message.

“the operand of a runtime dynamic_cast must have a polymorphic class type”

dynamic_cast<cladire*>(Locations[i]);

The operand Locations[i] must have a polymorphic class type. What is a polymorphic class? It is a class that has at least one virtual function.

Calin said:
I am using inheritance already. The syntax I'm using is this:

Are you sure you're implementing it correctly?

You can use non-virtual functions, but the compiler will statically bind the function to the type that it sees. If the pointer's type is the base class it will use the base class non-virtual function, if the pointer's type is the derived class it will use the derived class non-virtual function.

Also, the use of dynamic_cast<> is a code smell. Almost always it means the interface is bad and you aren't following SOLID principles. Generally it violates both the LSP and Dependency Inversion. Code should rely on the base class or interfaces and allow substitution by any derived class. If you have to use dynamic_cast<> to a derived class like this, it's a warning that you're likely doing something wrong. Instead of the system using the functionality make the decision about the class, place the function in the base class interface and implement the intended functionality in your leaf class.

Location must define at least one virtual method

This was the problem. Thank you.

My project`s facebook page is “DreamLand Page”

Thank you frob

My project`s facebook page is “DreamLand Page”

This topic is closed to new replies.

Advertisement