gpt4 book ai didi

c++ - 使用 boost::bind 的菱形继承(钻石问题)

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:54 24 4
gpt4 key购买 nike

我有这样的设计:

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=T混凝土 ]

知道这可能有什么问题吗?

最佳答案

你的继承图看起来像这样:

           Concrete 
|
D
/ \
C B
/ \
A A

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

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

           Concrete 
|
D
/ \
C B
\ /
A

关于c++ - 使用 boost::bind 的菱形继承(钻石问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19847104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com