Diamond多重继承与动态转换 [英] Diamond multiple inheritance vs. dynamic cast

查看:78
本文介绍了Diamond多重继承与动态转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意这个简单的多重继承模型:


void main(){


A级{

public:

virtual void print(){cout<< " A" << endl;

};


class支持1:虚拟公共A {

public:

virtual void print(){cout<< " supp1" << endl;

};


class Support2:虚拟公共A {

public:

virtual void print(){cout<< " supp2" <<结束;

};


B类:虚拟公众A,公众支持1,公众支持2 {

public:

virtual void print(){cout<< " supp1" <<结束;

};


A * ptr =新B;

B * lpb = dynamic_cast< B *>( ptr);


删除ptr;


}


....


我得到以下警告(并且程序崩溃):


警告c4541:''dynamic_cast''用于多态类型''main :: A'与

/ GR-;可能导致不可预测的行为

.....

嗯,为什么这不起作用。如果B类派生自A(由于虚拟而持有

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

推荐答案

ax **** @ gmail.com 写道:

请注意这个简单的多重继承模型:


void main(){
Please observe this simple model of multiple inheritance:

void main() {



1.非法。 main()*必须*返回int。

1. Illegal. main() *MUST* return int.


>



}


...


我收到以下警告(程序崩溃):


警告c4541 :''dynamic_cast''用于多态类型''main :: A''与

/ GR-;可能导致不可预测的行为


....


嗯,为什么这不起作用。如果B类派生自A(由于虚拟而持有

一个A的内部实例),那么动态演员
应该有效,对吧?

请帮帮我,谢谢。
>
[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.



因为微软认为RTTI和dynamic_cast太贵了而且b $ b昂贵。除非明确启用。

Because Microsoft decided that RTTI and dynamic_cast were "too
expensive" unless explicitly enabled.


ax****@gmail.com 写道:

请注意这个简单的多重继承模型:
Please observe this simple model of multiple inheritance:



#include< ; iostream>


使用std :: cout;

使用std :: endl;

#include <iostream>

using std::cout;
using std::endl;


void main(){
void main() {



int main(){

int main() {


class A {

public:

virtual void print(){cout<< " A" <<结束;

};
class A {
public:
virtual void print() { cout << "A" << endl;
};



在这个和所有后续的print()函数中,你忘了一个右大括号。

In this and all following print() functions, you forgot a closing brace.


class Support1 :虚拟公共A {

public:

virtual void print(){cout<< " supp1" << endl;

};


class Support2:虚拟公共A {

public:

virtual void print(){cout<< " supp2" <<结束;

};


B类:虚拟公众A,公众支持1,公众支持2 {

public:

virtual void print(){cout<< " supp1" <<结束;

};
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;
};



我假设你的意思是B不是supp1这里。

I assume you meant "B" not "supp1" here.


A * ptr = new B;

B * lpb = dynamic_cast< B *>(ptr);


删除ptr;
A *ptr = new B;
B *lpb = dynamic_cast<B *>(ptr);

delete ptr;



这里你是通过指向A的指针删除指向B的指针。在这种情况下你必须使用虚拟析构函数
,否则你将会有未定义的

行为。这很可能是导致你崩溃的原因。

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.


}


...


我得到以下警告(并且程序崩溃):


警告c4541:''dynamic_cast''用于多态类型''main :: A''with

/ GR-;不可预知的行为可能导致
}

...

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没有给出这样的警告。

g++ 4.1.1 gives no such warning.


嗯,为什么没有'这个工作。如果B类派生自A(由于虚拟而持有

一个A的内部实例),那么动态演员
应该有效,对吧?

请帮帮我,谢谢。
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.



我认为你的问题不是多重继承或者是dynamic_cast<> ;.


Nate


请注意这个简单的多重继承模型:
Please observe this simple model of multiple inheritance:

>

class A {

public:

virtual void print(){cout<< " A" << endl;

};


class支持1:虚拟公共A {

public:

virtual void print(){cout<< " supp1" << endl;

};


class Support2:虚拟公共A {

public:

virtual void print(){cout<< " supp2" <<结束;

};


B类:虚拟公众A,公众支持1,公众支持2 {

public:

virtual void print(){cout<< " supp1" <<结束;

};


A * ptr =新B;

B * lpb = dynamic_cast< B *>( ptr);


删除ptr;


}


...


我得到以下警告(并且程序崩溃):


警告c4541:''dynamic_cast''用于多态类型''main :: A ''

/ GR-;不可预测的行为可能导致
>
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



三件事:

1)请发布至少编译的代码。请参阅
http ://www.parashift.com/c++-faq-lit...t.html#faq-5.8 了解更多

发布信息。


2)你看到的警告是特定于Visual C ++的,所以

偏离主题。

< OT>

如果你在Visual Studio的帮助中查找C4541,你会发现这样的东西:

错误信息

''标识符''用于多态类型''类型''用/ GR-;

不可预测的行为可能导致


您试图使用需要运行的功能时间类型信息

没有启用运行时类型信息。用/ GR重新编译。


换句话说,更改编译设置以启用RTTI。这将是

修复警告。

< / OT>


3)即使警告已经修复,这仍然赢了不行。 A上需要一个

虚拟析构函数。参见
http://www.parashift.com/c++-faq-lit....html#faq-20.7



虚拟~A(){}


Michael


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


这篇关于Diamond多重继承与动态转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆