dynamic_cast: 'a' is not a polymorphic type

  • Thread starter Maurice Termeer
  • Start date
M

Maurice Termeer

Hi, suppose i've got this:

class a {
public:
int n;
};

class b : public a {
public:
};

and I next do this:

a *x = new b();
b *y = dynamic_cast<b *>(x);

If I compile this, the compiler (msvc++ 2005) says:

error C2683: 'dynamic_cast' : 'a' is not a polymorphic type

and gnu g++ 3.2.3 says:

cannot dynamic_cast `x' (of type `class a*') to type `class b*' (
source type is not polymorphic)

I don't understand this. Why can't I dynamically cast a pointer of type
base class to some derived class? Probably I have to tell the compiler
that class a has some subclasses. Anyone got an idea?

Maurice Termeer
 
S

Sharad Kala

Maurice Termeer said:
I don't understand this. Why can't I dynamically cast a pointer of type
base class to some derived class? Probably I have to tell the compiler
that class a has some subclasses. Anyone got an idea?

You got to have at least one virtual function in your base class to use
dynamic_cast or to make it polymorphic.

Sharad
 
R

Ron Natalie

Sharad Kala said:
You got to have at least one virtual function in your base class to use
dynamic_cast or to make it polymorphic.
You need to have a polymorphic class to use dynamic_cast IN THIS FASHION.

The presence of the virtual function in the base class is what makes it "polymorphic"
by DEFINITION.
 
J

John Harrison

Maurice Termeer said:
Hi, suppose i've got this:

class a {
public:
int n;

Add this

virtual ~a() {}

and it will work
};

class b : public a {
public:
};

and I next do this:

a *x = new b();
b *y = dynamic_cast<b *>(x);

If I compile this, the compiler (msvc++ 2005) says:

error C2683: 'dynamic_cast' : 'a' is not a polymorphic type

john
 
R

Rolf Magnus

Maurice said:
Hi, suppose i've got this:

class a {
public:
int n;
};

class b : public a {
public:
};

and I next do this:

a *x = new b();
b *y = dynamic_cast<b *>(x);

If I compile this, the compiler (msvc++ 2005) says:

error C2683: 'dynamic_cast' : 'a' is not a polymorphic type

and gnu g++ 3.2.3 says:

cannot dynamic_cast `x' (of type `class a*') to type `class b*' (
source type is not polymorphic)

I don't understand this. Why can't I dynamically cast a pointer of type
base class to some derived class?

Just as the compiler says: because your base class is not polymorphic, i.e.
doesn't use dynamic typing. Therefore, dynamic type casting can't work.
Probably I have to tell the compiler that class a has some subclasses.

No. You just have to make it polymorphic by adding a virtual member function
or making the destructor virtual.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top