C ++多继承函数调用模糊性 [英] C++ multiple inheritance function call ambiguity

查看:147
本文介绍了C ++多继承函数调用模糊性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与C ++中的多重继承相关的基本问题。如果我有一个如下所示的代码:

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'

是否无法调用函数 start()从一个特定的基类使用派生类对象?

Is there no way to be able to call function start() from a specific base class using a derived class object?

我不知道使用

推荐答案

a.base1::start();

a.base2::start();

或如果您想特别使用

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

这篇关于C ++多继承函数调用模糊性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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