@az
#include <iostream>
using namespace std;
class a
{
public:
bool bstatus;
a(){
bstatus = false;
cout << "class a:bstatus value address " << &bstatus <<endl;
}
void aa1(){}
void aa2(){}
};
class b : virtual private a
{
public:
a::bstatus;//回傳false or true
b(){
cout << "class b:bstatus value address " << &bstatus <<endl;
}
};
class c : virtual private a
{
public:
a::bstatus;//回傳false or true
c(){
cout << "class c:bstatus value address " << &bstatus <<endl;
}
};
class d : private b, private c
{
public:
b::bstatus; //或者c::bstatus 效果一样
d(){
cout << "class d:bstatus value address " << &bstatus <<endl;
}
};
int main()
{
d obj;
cout << obj.bstatus <<endl;
return 0;
}
输出结果:
class a:bstatus value address 0012FF7C
class b:bstatus value address 0012FF7C
class c:bstatus value address 0012FF7C
class d:bstatus value address 0012FF7C
0
-------------------
//bstatus内存地址一样, 既class a, class b, class c, class d共用class a的bStatus
@az
class d : public b, public c
{
public:
b::bstatus; //或者c::bstatus;
};
这样就避免了ambiguous引用