Diamond multiple inheritance vs. dynamic cast

A

axel22

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.
 
R

red floyd

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.
 
N

Nate Barney

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
 
M

Michael

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-lite/how-to-post.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-lite/virtual-functions.html#faq-20.7.
Something like
virtual ~A() {}

Michael
 
H

Howard

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
 
A

axel22

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?
 
A

axel22

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? :)
 
D

Default User

red floyd je napisao/la:

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
 
N

Noah Roberts

Default said:
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.
 
R

Ron Natalie

Noah said:
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.
 
R

red floyd

Ron said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top