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

Diamond multiple inheritance vs. dynamic cast

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

<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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.