简体   繁体   English

使用boost :: bind继承钻石

[英]Diamond inheritance with boost::bind

I have a design like this: 我有这样的设计:

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));
}

The compiler complains about the first boost::bind call. 编译器抱怨第一个boost :: bind调用。 The call to function inside C and D have no issues. C和D内部对函数的调用没有问题。 Here is the exact error: 这是确切的错误:

boost/bind/mem_fn_template.hpp(384) : error C2594: 'newline' : ambiguous conversions from 'Concrete *' to 'A *' with [ T=TConcrete ] boost / bind / mem_fn_template.hpp(384):错误C2594:'newline':使用[T = TConcrete]从'Concrete *'到'A *'的不明确转换

Any ideas what could be wrong with this? 任何想法这可能有什么问题吗?

Your inheritance diagram looks something like this: 您的继承图如下所示:

           Concrete 
              |
              D
             / \
            C   B
           /     \
          A       A

When you try to convert your Concrete* into an A* , the compiler has no idea which instance of A you want. 当您尝试将Concrete*转换为A* ,编译器不知道您需要哪个A实例。 Do you want to convert to the A that C derives from, or the A that B derives from? 你想转换成AC从派生,或AB从派生?

The solution is to use virtual inheritance to derive B and C from A , so that there is only one instance of A . 解决方案是使用虚拟继承从A派生BC ,以便仅存在A一个实例。

           Concrete 
              |
              D
             / \
            C   B
             \ /
              A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM