By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,916 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,916 IT Pros & Developers. It's quick & easy.

dynamic_cast: 'a' is not a polymorphic type

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Maurice Termeer" <m.*********@student.tue.nl> wrote in message
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
Jul 22 '05 #2

P: n/a

"Sharad Kala" <no*****************@yahoo.com> wrote in message news:2s*************@uni-berlin.de...

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.

Jul 22 '05 #3

P: n/a

"Maurice Termeer" <m.*********@student.tue.nl> wrote in message
news:cj**********@news.tue.nl...
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
Jul 22 '05 #4

P: n/a
Maurice Termeer wrote:
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.
Anyone got an idea?


Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.