C++多重继承函数调用歧义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6845854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ multiple inheritance function call ambiguity
提问by goldenmean
I have a basic question related to multiple inheritance in C++. If I have a code as shown below:
我有一个与 C++ 中的多重继承相关的基本问题。如果我有如下所示的代码:
struct base1 {
void start() { cout << "Inside base1"; }
};
struct base2 {
void start() { cout << "Inside base2"; }
};
struct derived : base1, base2 { };
int main() {
derived a;
a.start();
}
which gives the following compilation error:
这给出了以下编译错误:
1>c:\mytest.cpp(41): error C2385: ambiguous access of 'start'
1> could be the 'start' in base 'base1'
1> or could be the 'start' in base 'base2'
Is there no way to be able to call function start()
from a specific base class using a derived class object?
有没有办法能够start()
使用派生类对象从特定基类调用函数?
I don't know the use-case right now but.. still!
我现在不知道用例但是..仍然!
回答by dascandy
a.base1::start();
a.base2::start();
or if you want to use one specifically
或者如果你想特别使用一个
class derived:public base1,public base2
{
public:
using base1::start;
};
回答by Lightness Races in Orbit
Sure!
当然!
a.base1::start();
or
或者
a.base2::start();