多重继承范本类别 [英] Multiple Inheritance Template Class

查看:88
本文介绍了多重继承范本类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class messageA {
};

class messageB {
};

template<class T>
class queue {
public:
    virtual ~queue() {}
    void submit(T& x) {}
};

class A : public queue<messageA>, public queue<messageB>
{
};

int main()
{
    A aa;
    aa.submit(messageA());
    aa.submit(messageB());
}

我首先想到的是,上面的代码应该没问题,因为类A将包含2个重载的Submit函数,它们将接受messageA和messageB对象.

My first thought is, the above code should be fine, as class A will contains 2 overloaded submit functions, which will accept messageA and messageB object.

但是,编译器给我以下错误:

However, the compiler gives me the following error :

我可以知道为什么有歧义吗?在第一次提交呼叫中,我要呼叫messageA版本不是很明显吗?对于第二次提交呼叫,我要呼叫messageB版本吗?

May I know why there is an ambiguous? Isn't it is quite obvious that, for the 1st submit call, I want to call messageA version? For the 2nd submit call, I want to call messageB version?

------ Build started: Project: main, Configuration: Release Win32 ------
Compiling...
main.cpp
.\main.cpp(21) : error C2385: ambiguous access of 'submit'
        could be the 'submit' in base 'queue<messageA>'
        or could be the 'submit' in base 'queue<messageB>'
.\main.cpp(21) : error C3861: 'submit': identifier not found
.\main.cpp(22) : error C2385: ambiguous access of 'submit'
        could be the 'submit' in base 'queue<messageA>'
        or could be the 'submit' in base 'queue<messageB>'
.\main.cpp(22) : error C2664: 'queue<T>::submit' : cannot convert parameter 1 from 'messageB' to 'messageA &'
        with
        [
            T=messageA
        ]
.\main.cpp(22) : error C3861: 'submit': identifier not found

推荐答案

我目前没有编译器,但我想一个继承可能隐藏了另一个:编译器将使用

I have no compiler right now, but I guess one inheritance could hide the other : The compiler will use Koenig Lookup to find the right symbol, and if I remember correctly, once the compiler find a suitable symbol (i.e., a method called "submit"), it will stop searching for others in parent and/or outer scopes.

在这种情况下,我认为将在两个继承的类中搜索符号,但是如果没有确切的编译器(Visual C ++ 2003?2008?2010?),我猜不出更多.

In this case, I thought both inheriting classes would be searched for the symbol, but without your exact compiler (Visual C++ 2003 ? 2008 ? 2010 ?), I cannot guess much more.

经过深思熟虑,另一种可能性是编译器确实找到了两个符号,但无法决定调用哪个符号(在符号解析的那一刻,编译器仅关心符号名称,而不关心其确切原型).我相信最后一种解释是正确的.

After some thoughts, another possibility is that the compiler did find both symbols, but is unable to decide which to call (at that moment of symbol resolution, the compiler cares only for symbol name, not its exact prototype). I believe this last explanation to be the right one.

尝试在派生类中添加using语句:

Try adding using statements in your derived classes :

class A : public queue<messageA>, public queue<messageB>
{
   using queue<messageA>::submit ;
   using queue<messageB>::submit ;
} ;

将两个Submit方法直接带入A类范围.

to bring both the submit methods directly in the A class scope.

还请注意,您的Submit方法将消息作为非const引用,而在构造函数中,您的消息参数是临时的(因此是const r值).

Note, too, that your submit methods are taking messages as non-const reference, while in the constructor, your message parameters are temporaries (and thus, const r-values).

将主体重新写为:

int main()
{
    A aa;
    messageA mA ;
    messageA mB ;
    aa.submit(mA);
    aa.submit(mB);
}

可以帮助编译(这可以解释第22行的编译器错误).

could help compile (this could explain the compiler error on line 22).

或者您可以更改Submit方法的原型以接受const引用而不是非const引用.

Or you could change the prototype of your submit methods to accept const references instead of non-const references.

注意:仍然没有编译器,因此请尝试对代码进行脑力调试... :-P ...

Note: Still without compiler, so trying to brain-debug your code... :-P ...

这篇关于多重继承范本类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆