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