文字描述(下面的代码):我有一个提供类集合的库。对于每组类,我们有两个具体类型,( ClassA_Partial
, ClassA
), ( ClassB_Partial
, ClassB
) 等。每个都分别实现 ( Interface_Partial
, Interface
)。此外,Interface
is a Interface_Partial
和 each Class?
is a Class?_Partial
- 创建一个菱形继承模式,其中顶部是虚拟继承的。
为什么同时继承和Interface_Partial
时函数不明确?ClassA
ClassB
struct Interface_Partial
{
virtual ~Interface_Partial();
virtual void f() = 0;
};
struct Interface
:
virtual Interface_Partial
{
virtual void g() = 0;
};
struct ClassA_Partial : public virtual Interface_Partial
{
void f() {};
};
struct ClassA : public Interface, public virtual ClassA_Partial
{
void g() {};
};
struct ClassB_Partial : public virtual Interface_Partial
{
void f() {};
};
struct ClassB : public Interface, public virtual ClassB_Partial
{
void g() {};
};
struct MyClass : public ClassA, public ClassB
{ }; // error C2250: MyClass : ambiguous inheritance of 'void Interface_Partial::f(void)'
当我们不止一次继承一个公共接口时,为什么我们不能像我们通常做的那样消除歧义?例如
struct ClassX : public Interface_Partial { void f() {} };
struct ClassY : public Interface_Partial { void f() {} };
class Another : public ClassX, public ClassY
{};
void func()
{
// This is ok
Another a;
a.ClassX::f();
// Why would this not work?
// unambiguously refers to the one and only f() function
// inherited via ClassA
MyClass b;
b.ClassA::f();
}