62

Is it possible to have inheritance with no virtual methods? The compiler is saying that the following code is not polymorphic.

Example:

class A {
public:
    int a;
    int getA(){return a;};
}


class B : public A {
public:
    int b;
    int getB(){return b;};
}

In another class we are trying to downcast from an A object to a B object:

 A *a = ...;
 B *b = dynamic_cast<B*>(a)

but this gives the following compile-time error:

 cannot dynamic_cast ... (source type is not polymorphic)
m7913d
  • 10,244
  • 7
  • 28
  • 56
wfbarksdale
  • 7,498
  • 15
  • 65
  • 88
  • In addition to the accepted answer, you might want to checkout this question: When should static_cast, dynamic_cast and reinterpret_cast be used? (http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-and-reinterpret-cast-be-used) – yasouser Dec 12 '11 at 08:12

5 Answers5

90

Syntax errors non-withstanding, you cannot dynamic_cast a non-polymorphic type. static_cast is the cast you would use in this case, if you know that it is in fact an object of the target type.

The reason why: static_cast basically has the compiler perform a check at compile time "Could the input be cast to the output?" This is can be used for cases where you are casting up or down an inheritance hierarchy of pointers (or references). But the check is only at compile time, and the compiler assumes you know what you are doing.

dynamic_cast can only be used in the case of a pointer or reference cast, and in addition to the compile time check, it does an additional run time check that the cast is legal. It requires that the class in question have at least 1 virtual method, which allows the compiler (if it supports RTTI) to perform this additional check. However, if the type in question does not have any virtual methods, then it cannot be used.

The simplest case, and probably worthwhile if you're passing pointers around like this, is to consider making the base class's destructor virtual. In addition to allowing you to use dynamic cast, it also allows the proper destructors to be called when a base class pointer is deleted.

Dave S
  • 20,507
  • 3
  • 48
  • 68
38

You need at least one virtual method in a class for run-time type information (RTTI) to successfully apply dynamic_cast operator.

tenorsax
  • 21,123
  • 9
  • 60
  • 107
22

just make A destructor virtual (always do for any class just for safety).

user993954
  • 423
  • 4
  • 8
  • 12
    not for any class but for the class which is intended to be a base class – ParokshaX Mar 24 '14 at 05:50
  • wouldn't that be unsafe, because child destructor will be invoked instead of parent's destructor? A programmer could forget to call BaseClass::~BaseClass() and will be screwed because parent-portion won't be destroyed. – Kari Jun 23 '18 at 22:04
  • 1
    @Kari, parent (base) destructor is automatically called after the child destructor. No need to explicitly call this destructor. – m7913d Mar 18 '21 at 14:11
6

yes, dynamic_cast for non-polymorphic types are not allowed. The base class shall have at least one virtual method. Only then that class can be called as polymorphic.

This article explains a similar example: http://www.cplusplus.com/doc/tutorial/typecasting/

Murali Krishna
  • 301
  • 1
  • 7
4
A a;
B *b = dynamic_cast<B*>(a)

Here a is an object and b is a pointer.

Actually, upcasting and downcasting are both allowed in C++. But when using downcasting, 2 things should be pay attention to:

  1. The superclass should has at least one virtual method.
  2. Since superclass is "smaller" than subclass, one should use memory object carefully.
m7913d
  • 10,244
  • 7
  • 28
  • 56
tyger
  • 181
  • 4