问题描述
- c++虚基类二义性的问题
- #include
using namespace std;
class A{
public:
A(int a):x(a){ cout<<""A constructor...""<<x<<endl; }
int f(){return ++x;}
~A(){cout<<""destructor A...""<<endl;}
private:
int x;
};
class B:public virtual A{
private:
int y;
A Aobj;
public:
B(int aint bint c):A(a)y(c)Aobj(c){ cout<<""B constructor...""<<y<<endl;}
int f(){
A::f();
Aobj.f();
return ++y;
}
void display(){ cout<<A::f()<<""t""<<Aobj.f()<<""t""<<f()<<endl; }
~B(){cout<<""destructor B...""<<endl;}};
class C:public B{
public:
C(int aint bint c):B(abc)A(0){ cout<<""C constructor...""<<endl;}
};
class D:public Cpublic virtual A{
public:
D(int aint bint c):C(abc)A(c){ cout<<""D constructor...""<<endl;}
~D(){cout<<""destructor D....""<<endl;}
};
int main()
{
D d(789);
d.f();
d.display();
return 0;
}main 中d.f(); 语句调用时 D类中有公有继承B类中的f();方法 和公有虚继承A 中的f();为什么不会产生二义性; 谢谢
时间: 2024-10-26 23:34:56