当多个基类具有同名成员函数时,如何解决函数调用歧义?

2024-04-07

我有一个与 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();
}

这给出了以下编译错误:

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'

有没有办法可以调用函数start()从使用派生类对象的特定基类?

我现在不知道用例但是..仍然!


a.base1::start();

a.base2::start();

或者如果你想专门使用一个

class derived:public base1,public base2
{
public:
    using base1::start;
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当多个基类具有同名成员函数时,如何解决函数调用歧义? 的相关文章

随机推荐