【发布时间】:2012-07-16 16:30:20
【问题描述】:
-编辑-
感谢您的快速响应,我的代码遇到了非常奇怪的问题,我将强制转换更改为 dynamic_cast 并且它现在可以正常工作
-原帖-
将一个基类的指针转换为另一个基类是否安全?稍微扩展一下,我在以下代码中标记的指针不会导致任何未定义的行为吗?
class Base1
{
public:
// Functions Here
};
class Base2
{
public:
// Some other Functions here
};
class Derived: public Base1, public Base2
{
public:
// Functions
};
int main()
{
Base1* pointer1 = new Derived();
Base2* pointer2 = (Base2*)pointer1; // Will using this pointer result in any undefined behavior?
return 1;
}
【问题讨论】:
-
你应该使用
dynamic_cast,而不是C风格的演员。 -
哦,
Base2是继承私有的,还是转录事故?如果是私人的,我的回答是不正确的! -
应该是public继承的,我打错了