小编6yr*_*y6e的帖子

为什么const/non-const函数的继承过载不明确?

我试图创建两个类,第一个使用非const实现的函数,第二个使用const实现.这是一个小例子:

class Base {
protected:
  int some;
};

class A : public virtual Base {
  const int& get() const {
    return some;
  }
};

class B : public virtual Base {
  int& get() {
    return some;
  }
};

class C : public A, B {};

C test;
test.get(); // ambiguous 
Run Code Online (Sandbox Code Playgroud)

get函数的调用是模糊的.无论const版本需要满足更多要求.(get对const的调用C也是模棱两可的,但是有一个可能的函数可以调用.)标准中有这种行为的原因吗?谢谢!

c++ inheritance overloading multiple-inheritance

19
推荐指数
2
解决办法
619
查看次数