473,888 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic_cast: 'a' is not a polymorphic type

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
4 27693

"Maurice Termeer" <m.*********@st udent.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

"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 "polymorphi c"
by DEFINITION.

Jul 22 '05 #3

"Maurice Termeer" <m.*********@st udent.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2848
by: Andreas Sch. | last post by:
Hello, I had been quite suprised by the following little program: ---- cut ---- #include "iostream.h" class base {
11
3328
by: Jamie Burns | last post by:
Hello, I just did a simple benchmark: for (xx=0;xx<100000;xx++) { rDerived* derived = dynamic_cast<rDerived*>(object); if (derived) derived->setValue(message.data.messageSetInt.value); } against:
14
2216
by: jimmy | last post by:
Hi, Please forgive me if this is elementary. I can't seem to find the solution anywhere. // foo() is only declared/defined in Derived Base* base = new Derived(); ((Derived*)base)->foo(); Given that I would like to use a Base*; is casting the only solution?
20
2242
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally, cast to void *. Specifically, template <typename T> void f(T * t) { void * p = dynamic_cast<void *>(t) ; } will not compile if T isn't of a class that has somewhere at least
10
3688
by: richardclay09 | last post by:
Please take a look at this method: template<class C> void f(C* ptrAny) { Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny); if(ptrFruit) { // do something specific to fruits } // Carry on using ptrAny whatever it is }
51
4586
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
4
1708
by: Osamede.Zhang | last post by:
Compiler store a pointer table in each object with virtual fuction to implement polymorphic,isn't it? But where is the pointer table? It seems that object only store a pointer to the table,the output of this code is 88. #include<iostream> using namespace std; class MyClass{ public: MyClass(){ a=0;} virtual void tst1() {a-=1;}
4
2378
by: David Sanders | last post by:
Hi, I have a class depending on a template parameter: template<int n> class MyClass { }; I want the template parameter to be chosen according to a variable, e.g.
10
3611
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the derived type 3 - this function then 'grows' the derived type object (by pushing onto a vector).
0
9959
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7988
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5812
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4638
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4241
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3246
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.