让我们从代码片段开始:
#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) 下无法观察到该问题