473,573 Members | 2,851 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 8886
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
3271
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
6462
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...
4
1426
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...
5
2467
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
2746
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
2409
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
2235
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...
9
10958
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
5714
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
7784
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...
0
7705
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...
0
8033
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7796
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...
1
5601
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...
0
5294
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...
0
3739
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2224
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
1
1316
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.