C++ multiple inheritance function call ambiguity

这一生的挚爱 提交于 2019-11-26 17:28:44

问题


I have a basic question related to multiple inheritance in C++. If I have a code as shown below:

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?

I don't know the use-case right now but.. still!


回答1:


a.base1::start();

a.base2::start();

or if you want to use one specifically

class derived:public base1,public base2
{
public:
    using base1::start;
};



回答2:


Sure!

a.base1::start();

or

a.base2::start();


来源:https://stackoverflow.com/questions/6845854/c-multiple-inheritance-function-call-ambiguity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!