简体   繁体   English

C ++多重继承-带模板的菱形

[英]C++ multiple inheritance - diamond with templates

I've got a problem when inheriting from multiple instances of a template. 从模板的多个实例继承时,我遇到了问题。

My class Bridge tries to inherit from 2 instances of BridgeTemplate, so when we try to call the BridgeTemplate's 'set' function, the compiler rises an error ("ambiguous..."). 我的类Bridge尝试从BridgeTemplate的2个实例继承,因此当我们尝试调用BridgeTemplate的'set'函数时,编译器会引发错误(“歧义...”)。 However, everything works ok if Bridge inherits from only 1 instance. 但是,如果Bridge仅从1个实例继承,则一切正常。

Below, a piece of code from both the template and class Bridge. 下面是模板和类Bridge的一部分代码。 Thanks in advance 提前致谢

template <class DataType, class DataWriter>
class BridgeTemplate : public BridgeGeneric
{
    public:
        void set(DataType a, DataWriter b)
        {
            std::cout << "a: " << a << "; b: " << b << std::endl;
        }
...
};

class Bridge : public virtual BridgeTemplate<int,float>, public virtual BridgeTemplate<float,int>
{
...
}

Argument types do not matter. 参数类型无关紧要。

The error message applies to name lookup, not to overload resolution. 该错误消息适用于名称查找,不适用于重载解析。 All overloaded functions must come from the same class or namespace. 所有重载的函数必须来自相同的类或名称空间。 In order to insure that, use this pattern: 为了确保这一点,请使用以下模式:

 class Child : public Dad, public Mom {
     using Dad::func;
     using Mom::func;
 };

 // ...

 Child c;
 c.foo(1, 2.3);

Because of the using declarations, both func members are brought to the Child namespace and the lookup is no longer ambiguous. 由于using声明,两个func成员都被带到Child命名空间,并且查找不再是模棱两可的。

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

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