首页 > 解决方案 > 当您在具有相同基类的派生类之间进行动态转换时会发生什么?

问题描述

我试图弄清楚当你dynamic_cast从一个派生类到另一个派生类时会发生什么。为什么下面的代码会引发分段错误?请注意,我并没有尝试将此代码用于任何事情。我只是想了解正在发生的事情。

还值得注意的是,相同的代码使用static_cast. 我似乎找不到任何文档来详细说明这里发生的事情。有人可以解释一下吗?

struct base 
{ 
    virtual void printing(){cout<<"base printing"<<endl;};
};
struct derived_1 :public base 
{ 
    virtual void printing(){cout<<"derived_1 printing"<<endl;};
};
struct derived_2 :public base 
{ 
    virtual void printing(){cout<<"derived_2 printing"<<endl;};
};

int main()
{
    base * b = new derived_2();
    derived_1 *d_1 = dynamic_cast<derived_1*>(b);

    // calling printing raises segmentation fault
    d_1->printing(); 
}

标签: c++11polymorphismdynamic-caststatic-cast

解决方案


转换为derived_1将失败,因为derived_2 是一个 base对象但不是一个derived_1对象。因此,您不能“转换”到所需的指针类型。

并不是说无论何时dynamic_cast失败,它都会返回一个nullptr引用类型除外)。这最终会导致代码中的分段错误(通常,我建议您始终if (d_1 != nullptr)在使用动态转换的对象之前添加 a )。

更新:

顺便说一句,这实际上是一个很好的例子dynamic_cast。即使您可能很想static_cast在您的示例中使用它并且它会编译,但您将处理未定义的行为。Usingstatic_cast将在没有打嗝的情况下编译,但实际上您将使用损坏的类型。假设derived_1::printing()访问一些变量derived_1::a,该变量在derived_2. derived_2通过将一个对象(没有)静态转换a为一个derived_1对象d_1,你会错误地假设d_1包含一些有效a的,事实并非如此。

例子:

// ...
struct derived_1 :public base
{
    const int a = 123;
    void printing() override {cout<<"derived_1 printing " << endl;}
    void foo() { cout << "Foo using constant = " << a << endl; }
};

// ...
int main()
{
    base * b = new derived_2();
    derived_1 *d_1 = static_cast<derived_1*>(b);
    d_1->printing(); // Will call derived_2::printing(), which is not what you expect!
    d_1->foo();      // Won't show a = 123 but some nonesense (mostly 0),
                     // because a is not defined in derived_2.
}

推荐阅读