【问题标题】:gcc 4.5.1 virtual inheritance issuegcc 4.5.1 虚拟继承问题
【发布时间】:2012-02-08 23:11:40
【问题描述】:

让我们从代码sn-p开始:

#include <iostream>

struct God{
    God(){_test = 8;}
    virtual ~God(){}
    int _test;
};

struct Base1 : public virtual God{
    //Base1(){std::cout << "Base1::Base1" << std::endl;}  //enable this line to fix problem
    virtual ~Base1(){}
};

struct Base2 : public virtual Base1{
    virtual ~Base2(){}
};

struct A1 : public virtual Base2{
    A1(){std::cout << "A1:A1()" << std::endl;}
    virtual ~A1(){};
};

struct A2 : public virtual Base2{
    A2(){std::cout << "A2:A2()" << std::endl;}
    virtual ~A2(){};
};


struct Derived: public virtual A1, public virtual A2{
    Derived():Base1(){std::cout << "Derived::Derived()" << std::endl;}
    Derived(int i){std::cout << "Derived(i)::Derived(i)" << std::endl;}         
    virtual ~Derived(){}
};


int main(){

    God* b1 = new Derived();
    std::cout << b1->_test << std::endl;    //why it prints 0?

    God* b2 = new Derived(5);
    std::cout << b2->_test << std::endl;

    return 0;
}

使用 GCC 4.5.1 和 4.6.1 编译 Derived 类的构造函数之间的唯一区别是第一个显式声明应该调用哪个 Base1 构造函数。 我希望 main() 中的两个 cout 都打印 8。不幸的是,第一个打印 0!。

为什么?

如果我启用 Base1 构造函数的显式定义,它可以解决问题。 如果我在派生类定义(派生类:public A1,public A2)中删除虚拟继承,它也可以工作。 这是预期的行为吗?

在 GCC 3.4.4 或 Microsoft 编译器 (VS) 下无法观察到该问题

【问题讨论】:

  • 我用 gcc 3.4.6 得到“8”
  • 当然看起来你已经触发了我的 gcc 错误。

标签: c++ virtual-inheritance


【解决方案1】:

这一定是编译器错误。我还测试了 GCC 4.2.1,结果都是 8 两种情况。

【讨论】:

    【解决方案2】:

    我测试了几种较旧的版本(最高 4.3 - 可行) - 手头没有 4.4 系列(将在早上测试),但我同意,这似乎是一个错误(而不是在db),可能值得提高它。

    我注意到,如果您将第一个关系(Base1 到 God 从虚拟更改为非虚拟),那么该示例将按预期工作。我想这取决于您的需求,您是否会直接从最派生的类中调用God 的ctor(尽管我以前从未见过这种方法...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 2012-10-11
      • 2016-10-26
      • 2019-12-20
      • 2017-12-09
      • 2013-08-24
      相关资源
      最近更新 更多