钻石继承与提升::绑定

Diamond inheritance with boost::bind

本文关键字:绑定 继承 钻石      更新时间:2023-10-16

我有这样的设计:

template <class T>
class A
{
};
template <class T>
class B : public A<T> 
{
};
template <class T>
class C : public A<T> 
{
};
template <class T>
class D : public C<T>, public B<T> 
{
};
struct TConcrete {
int xyz;
};
class Concrete : public D<TConcrete>
{
void Foo();
};
void
Concrete::Foo()
{
Bar (boost::bind(&A::Afunc, this, _1, _2), boost::bind(&C::Cfunc, this, _1, _2),     boost::bind(&D::Dfunc, this, _1, _2));
}

编译器抱怨第一个 boost::bind 调用。在 C 和 D 中调用函数没有问题。这是确切的错误:

boost/bind/mem_fn_template.hpp(384):错误 C2594:"换行符":从"具体 *"转换不明确到"A *" 跟 [ T=混凝土 ]

任何想法这可能有什么问题?

您的继承图如下所示:

           Concrete 
              |
              D
             / 
            C   B
           /     
          A       A

当您尝试将Concrete*转换为A*时,编译器不知道您想要哪个A实例。是否要转换为C派生自的AB派生自的A

解决方案是使用虚拟继承从A派生BC,这样A只有一个实例。

           Concrete 
              |
              D
             / 
            C   B
              /
              A