//二义性 #include <iostream> using namespace std; class Base{ public: Base(){ cout << "Base called..." << endl; } void print() { cout << "Base ::print()" << endl; } }; class Sub{ public: Sub(){ cout << "Sub called..." << endl; } void print(){ cout << "Sub print...." << endl; } }; class Child: public Base, public Sub{ public: Child(){ cout << "Child called..." << endl; } }; int main(void){ Child c; //c.print();报错,这里c类中的有两个print()函数,它们的地位相同 c.Base::print(); c.Sub::print(); return 0; }
//砖石继承,二义性
#include <iostream> using namespace std; int gFlag = 0; class Base{ public: Base(){ cout << "Base::Base()" <<++gFlag << endl; } void print(){ cout << "Base::print()" << endl; } }; class Mid1:public Base{ public: Mid1(){ cout << "Mid1:Mid1()" << endl; } }; class Mid2:public Base{ public: Mid2(){ cout << "Mid2:Mid2()" << endl; } }; class Child:public Mid1,public Mid2{ public: Child(){ cout << "Child:Child()" << endl; } }; int main(void){ Child d; //d.print() << endl;报错,这样使用会产生二义性 d.Mid1::print();//ok d.Mid2::print();//ok return 0; }
Base::Base()1
Mid1::Mid1()
Base::Base()2
Mid2::Mid2()
Child::Child()
在实例化,Child对象时,先会调用Mid1和Mid2构造函数,Mid1和Mid2分别继承与Base,所以
Base最终被调用了两次.
虚继承
#include <iostream> using namespace std; int gFlag = 0; class Base{ public: Base(){ cout << "Base::Base()"<<++gFlag << endl; } void print(){ cout << "Base::print()" << endl; } }; class Mid1:virtual public Base{ public: Mid1(){ cout << "Mid1::Mid1()" << endl; } }; class Mid2:virtual public Base{ public: Mid2(){ cout << "Mid2::Mid2()" << endl; } }; class Child:public Mid1,public Mid2{ public: Child(){ cout << "Child::Child()" << endl; } }; int main(void){ Child d; d.print(); return 0; }
Base::Base()1
Mid1::Mid1()
Mid2::Mid2()
Child::Child()
Base::print()
虚继承,会共享子类通过虚继承的基类的内存,Mid1调用实例化了一次Base,Mid2构造时,就没有再次
实例化Base,Mid1和Mid2共享基类Base的内存.
虚继承,解决了二义性问题,也节省了内存.
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。