473,395 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 8870
ax****@gmail.com 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.com 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.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.com 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.com 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
Noah Roberts wrote:
Unless you're told not to by some bozo who believes MS's BS and has say
so.
Even Microsoft has seen the light. I think Visual Studio 6 as the
last version to come with RTTI off by default.
Oct 5 '06 #11
Ron Natalie wrote:
Noah Roberts wrote:
>Unless you're told not to by some bozo who believes MS's BS and has say
so.
Even Microsoft has seen the light. I think Visual Studio 6 as the
last version to come with RTTI off by default.
Nope, 7.1 still has it off by default. 8.0 (2005) enables it. The point
is, however, that it shouldn't have to be "enabled". It's part of the
language.

Oct 5 '06 #12

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

Similar topics

13
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...
6
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...
4
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...
5
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
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...
2
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...
11
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...
9
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.