473,708 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Diamond multiple inheritance vs. dynamic cast

Please observe this simple model of multiple inheritance:

void main() {

class A {
public:
virtual void print() { cout << "A" << endl;
};

class Support1 : virtual public A {
public:
virtual void print() { cout << "supp1" << endl;
};

class Support2 : virtual public A {
public:
virtual void print() { cout << "supp2" << endl;
};

class B : virtual public A, public Support1, public Support2 {
public:
virtual void print() { cout << "supp1" << endl;
};

A *ptr = new B;
B *lpb = dynamic_cast<B *>(ptr);

delete ptr;

}

....

I get the following warning (and the program crashes):

warning c4541: 'dynamic_cast' used on polymorphic type 'main::A' with
/GR-; unpredictable behaviour may result
.....
Well, why doesn't this work. If class B is derived from A (and holds
one internal instance of A because of virtual), then a dynamic cast
should work, right?
Please help me with this and thank you.

Oct 4 '06 #1
11 8902
ax****@gmail.co m wrote:
Please observe this simple model of multiple inheritance:

void main() {
1. Illegal. main() *MUST* return int.
>
[redacted]
}

...

I get the following warning (and the program crashes):

warning c4541: 'dynamic_cast' used on polymorphic type 'main::A' with
/GR-; unpredictable behaviour may result
....
Well, why doesn't this work. If class B is derived from A (and holds
one internal instance of A because of virtual), then a dynamic cast
should work, right?
Please help me with this and thank you.
Because Microsoft decided that RTTI and dynamic_cast were "too
expensive" unless explicitly enabled.

Oct 4 '06 #2
ax****@gmail.co m wrote:
Please observe this simple model of multiple inheritance:
#include <iostream>

using std::cout;
using std::endl;
void main() {
int main() {
class A {
public:
virtual void print() { cout << "A" << endl;
};
In this and all following print() functions, you forgot a closing brace.
class Support1 : virtual public A {
public:
virtual void print() { cout << "supp1" << endl;
};

class Support2 : virtual public A {
public:
virtual void print() { cout << "supp2" << endl;
};

class B : virtual public A, public Support1, public Support2 {
public:
virtual void print() { cout << "supp1" << endl;
};
I assume you meant "B" not "supp1" here.
A *ptr = new B;
B *lpb = dynamic_cast<B *>(ptr);

delete ptr;
Here you are deleting a pointer to B through a pointer to A. You must
use virtual destructors in this case, or you will have undefined
behavior. This is most likely what is causing your crash.
}

...

I get the following warning (and the program crashes):

warning c4541: 'dynamic_cast' used on polymorphic type 'main::A' with
/GR-; unpredictable behaviour may result
g++ 4.1.1 gives no such warning.
Well, why doesn't this work. If class B is derived from A (and holds
one internal instance of A because of virtual), then a dynamic cast
should work, right?
Please help me with this and thank you.
I don't think your problem is with multiple inheritance or dynamic_cast<>.

Nate
Oct 4 '06 #3
Please observe this simple model of multiple inheritance:
>
class A {
public:
virtual void print() { cout << "A" << endl;
};

class Support1 : virtual public A {
public:
virtual void print() { cout << "supp1" << endl;
};

class Support2 : virtual public A {
public:
virtual void print() { cout << "supp2" << endl;
};

class B : virtual public A, public Support1, public Support2 {
public:
virtual void print() { cout << "supp1" << endl;
};

A *ptr = new B;
B *lpb = dynamic_cast<B *>(ptr);

delete ptr;

}

...

I get the following warning (and the program crashes):

warning c4541: 'dynamic_cast' used on polymorphic type 'main::A' with
/GR-; unpredictable behaviour may result

Three things:
1) Please post code that at least compiles. See
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8 for more
info on posting.

2) The warning you are seeing is Visual C++ specific, and so is
off-topic.
<OT>
If you were to look up C4541 in the help for Visual Studio, you would
find something like this:
Error Message
'identifier' used on polymorphic type 'type' with /GR-;
unpredictable behavior may result

You tried to use a feature that requires run-time type information
without enabling run-time type information. Recompile with /GR.

In other words, change your compile settings to enable RTTI. That will
fix the warning.
</OT>

3) Even with the warning fixed, this still won't work. You need a
virtual destructor on A. See
http://www.parashift.com/c++-faq-lit....html#faq-20.7.
Something like
virtual ~A() {}

Michael

Oct 4 '06 #4

<ax****@gmail.c omwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Please observe this simple model of multiple inheritance:

void main() {

class A {
public:
virtual void print() { cout << "A" << endl;
};

class Support1 : virtual public A {
public:
virtual void print() { cout << "supp1" << endl;
};

class Support2 : virtual public A {
public:
virtual void print() { cout << "supp2" << endl;
};

class B : virtual public A, public Support1, public Support2 {
public:
virtual void print() { cout << "supp1" << endl;
};

A *ptr = new B;
B *lpb = dynamic_cast<B *>(ptr);

delete ptr;

}

...

I get the following warning (and the program crashes):

warning c4541: 'dynamic_cast' used on polymorphic type 'main::A' with
/GR-; unpredictable behaviour may result
Look up the flag "/GR-". That's why you're getting the warning, I assume.
(As for the crash, see the other replies.)

-Howard
Oct 4 '06 #5
red floyd je napisao/la:
>
Because Microsoft decided that RTTI and dynamic_cast were "too
expensive" unless explicitly enabled.
Thnx, floyd. It seems I'll have to find some other sort of a solution
for this.

One more question though.

If I were to obtain a pointer to a derived class, and then pass it's
value (with operator=)
to a pointer to a base class, would they then have the same address?

Oct 4 '06 #6
Well, thank you all for your replies. As for the offtopic
note, if I knew it was offtopic, than I probably wouldn't ask, right? :)

Oct 4 '06 #7
ax****@gmail.co m wrote:
red floyd je napisao/la:

Because Microsoft decided that RTTI and dynamic_cast were "too
expensive" unless explicitly enabled.

Thnx, floyd. It seems I'll have to find some other sort of a solution
for this.
You should be able to turn that functionality on. Somewhere in the
project settings (getting OT here) there should be an option to enable
RTTI.


Brian
Oct 4 '06 #8

Default User wrote:
ax****@gmail.co m wrote:
red floyd je napisao/la:
>
Because Microsoft decided that RTTI and dynamic_cast were "too
expensive" unless explicitly enabled.
Thnx, floyd. It seems I'll have to find some other sort of a solution
for this.

You should be able to turn that functionality on. Somewhere in the
project settings (getting OT here) there should be an option to enable
RTTI.
Unless you're told not to by some bozo who believes MS's BS and has say
so.

Oct 4 '06 #9
Noah Roberts wrote:
>
Default User wrote:
You should be able to turn that functionality on. Somewhere in the
project settings (getting OT here) there should be an option to
enable RTTI.

Unless you're told not to by some bozo who believes MS's BS and has
say so.

Always a possibility, of course.


Brian
Oct 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3286
by: John Perks and Sarah Mount | last post by:
Trying to create the "lopsided diamond" inheritance below: >>> class B(object):pass >>> class D1(B):pass >>> class D2(D1):pass >>> class D(D1, D2):pass Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution
6
6483
by: Stuart Golodetz | last post by:
Hi, I've got a minor casting issue which I need to check about (marked // <--). I was trying to do a static_cast on it (which didn't work, though I'm not sure why this should be the case?) I also tried reinterpret_cast (which is clearly an exceedingly dodgy thing to do; it worked, but I'm not sure whether it should have worked, or whether (the more likely scenario) it was just coincidence?) Finally, after a bit of trawling through the...
4
1431
by: Steven T. Hatton | last post by:
Below is some code I wrote to get a better understanding of the dynamic verses static type resolution. My intention was to see if there was a way to use references for the static access to the subobjects of an object of derived type. I thought I had seen a way to use references to statically access member functions in a way syntactically similar to how pointers dynamically access the same objects member functions. The overall objective...
5
2472
by: Tony Johansson | last post by:
Hello Experts! It it correct to say that a solution to the diamond problem is to use virtual inheritance with virtual base classes. //Tony
4
2754
by: Alex Hunsley | last post by:
I've seen a few discussion about the use of 'super' in Python, including the opinion that 'super' should only be used to solve inheritance diamond problem. (And that a constructor that wants to call the superclass methods should just call them by name and forget about super.) What is people's opinion on this? Does it make any sense?
2
2415
by: emma middlebrook | last post by:
Hi I come from a C++ background and so am familiar with the 'dreaded inheritance diamond' i.e. the ambiguity of which data to use when a class appears twice in the hierarchy. Here's a link for anyone not familiar: http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 .. Base / \
11
2247
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern to illustrate this. Problem: I need to expose two lists of objects from a high-level class. I would like to expose these lists as read-only, but require write access internally.
9
10969
by: weird0 | last post by:
How does C++ and C# solve the Diamond problem? With the help of interfaces that is. Can anyone elaborate ....... Regards
6
5724
by: Rocketmagnet | last post by:
Hi all, I have been kind of forced (honestly) into writing a class structure which contains a Diamond of Death, and I'm not entirely sure what to do about it. This is a simplified version of my class structure: class entity { public:
0
8697
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,...
0
9159
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9061
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
7925
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...
0
5939
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4454
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...
0
4713
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3151
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
2508
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.