Quantcast
Channel: Can't downcast because class is not polymorphic? - Stack Overflow
Viewing all articles
Browse latest Browse all 6

Can't downcast because class is not polymorphic?

$
0
0

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)

Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images