首页 > 代码库 > c++,多继承造成的二义性及解决办法

c++,多继承造成的二义性及解决办法

#include <iostream>using namespace std;//-------------------------------class A1{public:    int a;public:    void m();};void A1::m() {    cout<<"A1::m():a="<<this->a<<endl;}//-------------------------------class A2 {public:    int a;    void m();};void A2::m() {    cout<<"A2::m(),a="<<this->a<<endl;}//-------------------------------class B :public A1, public A2{public:    void show();};void B::show(){    cout<<"A1::a="<<this->A1::a<<endl;    cout<<"A2::a="<<this->A2::a<<endl;}//-------------------------------void f1() {    B b;        b.A1::a = 34;    b.A2::a = 32432;        b.A1::m();//这时不能用b.m(),具有歧义;    b.A2::m();//用格式 b.A1::m(), b.A2::m()明确对象,消除歧义    b.show();}int main() {    f1();    while(1);    return 0;}
/*测试结果:

A1::m():a=34
A2::m(),a=32432
A1::a=34
A2::a=32432

*/

 

c++,多继承造成的二义性及解决办法