多重虚拟继承歧义
Multiple virtual inheritance ambiguity
最好用一个例子来解释:
struct A
{
virtual void foo() = 0;
};
struct B
{
virtual void foo() = 0;
};
struct C : A, B
{
// Implementation of A::foo
void A::foo();
// Implementation of B::foo
void B::foo();
};
// Define here; howto?
我知道我可以定义与类内联的函数,但这在这种情况下是不可行的(有大约6个基类实现所有40+方法)
,
显然我没有讲清楚。直接的问题是:如何在不引起歧义问题的情况下定义继承的foo()方法?
void C::foo() { /* etc */ }
这将定义A::foo()的实现,但B::foo()呢?请记住,我不希望在c的声明中定义方法。
注:,以这种方式建模问题的原因是预先定义的(COM/OLE)
,
这是很好的(MSVC),尽管内联:
struct A { virtual int foo() = 0; };
struct B { virtual int foo() = 0; };
struct C : A, B
{
int A::foo() { return 1; }
int B::foo() { return 2; }
};
void main()
{
C* p = new C();
cout << ((A*) p)->foo();
cout << ((B*) p)->foo();
}
您不能为两个在两个基类中定义了相同签名的函数提供单独的覆盖。(你也不能像你认为的那样,在类中内联地定义它们)。
一种可能是引入中间基类来重命名函数:
struct ABodge : A
{
void foo() {A_foo();}
virtual void A_foo() = 0;
};
struct BBodge : B
{
void foo() {B_foo();}
virtual void B_foo() = 0;
};
struct C : ABodge, BBodge
{
void A_foo();
void B_foo();
};
或者,可以通过组合两个内部类来避免多重继承,每个内部类实现一个基类接口:
class C
{
public:
// Could probably be defined as `operator A&()`, etc. to look more like
// inheritance, but I usually prefer conversions to be explicit.
A & asA() {return a;}
A const & asA() const {return a;}
B & asB() {return b;}
B const & asB() const {return b;}
private:
// These might need back-references to the 'C' that contains them.
struct AImpl : A {void foo();} a;
struct BImpl : B {void foo();} b;
};
工作正常:
struct A { virtual void foo() = 0; };
struct B { virtual void foo() = 0; };
struct C : A, B { virtual void foo(); };
void C::foo() { }
对于任何C x;
, static_cast<A&>(x).foo()
和static_cast<B&>(x).foo()
都将按预期调用x.foo()
,这正是A
和B
的纯虚接口所承诺的。
相关文章:
- 继承函数的重载解析
- 继承期间显示未知行为的子类
- 多重继承相同的方法名,没有歧义
- 使用enable_if解决多重继承歧义
- 通过继承重载运算符会导致歧义
- 解决虚拟方法的歧义继承的两种方法
- 如何正确消除继承类型定义的歧义?以及如何简化他们的创作?
- 在C 中,静态成员函数是否继承了?如果是,为什么没有出现歧义错误
- 引用如何解决多重继承中的歧义
- C++私有变量名称和继承歧义
- 如何处理同一模板多重继承中的歧义
- 消除多重继承中类成员的歧义
- 为什么私有继承不能解决静态函数的歧义?(在 MSVC 中测试)
- 与 [] 运算符和多重继承的歧义
- 关于多重继承和歧义
- C++多重继承+虚拟函数(-歧义)=怪异行为(也是函数指针)
- 数据成员访问歧义和菱形继承
- 多重虚拟继承歧义
- c++多重继承有歧义
- 多重继承歧义基类