Questions tagged «virtual-inheritance»


5
虚拟继承如何解决“钻石”(多重继承)的歧义?
class A { public: void eat(){ cout<<"A";} }; class B: virtual public A { public: void eat(){ cout<<"B";} }; class C: virtual public A { public: void eat(){ cout<<"C";} }; class D: public B,C { public: void eat(){ cout<<"D";} }; int main(){ A *a = new D(); a->eat(); } 我了解钻石的问题,并且上面的代码没有这个问题。 …

1
为什么在虚拟继承中调用Default构造函数?
我不明白为什么在下面的代码中,当我实例化一个类型为object的对象时daughter,grandmother()会调用默认构造函数? 我认为grandmother(int)应该调用构造函数(以遵循我的mother类构造函数的规范),或者由于虚拟继承而根本不应该编译此代码。 在这里,编译器grandmother在我的背后默默地调用默认构造函数,而我从未要求过它。 #include <iostream> class grandmother { public: grandmother() { std::cout << "grandmother (default)" << std::endl; } grandmother(int attr) { std::cout << "grandmother: " << attr << std::endl; } }; class mother: virtual public grandmother { public: mother(int attr) : grandmother(attr) { std::cout << "mother: " << attr << …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.