小编Rap*_*ael的帖子

将指针从一种基类型转换为另一种基类型

-编辑-

感谢您的快速响应,我的代码遇到了非常奇怪的问题,我将我的演员阵容更改为dynamic_cast,现在它完美地工作了

-ORIGINAL POST-

将一个基类的指针强制转换为另一个基类是否安全?为了扩展这一点,我在下面的代码中标记的指针是否会导致任何未定义的行为?

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;
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers

8
推荐指数
1
解决办法
1786
查看次数

标签 统计

c++ ×1

pointers ×1