在 C++ 中混合覆盖和重载

Syd*_*.Ka 2 c++ debugging computer-science

我不明白为什么error: no matching function for call to ‘Child::m(Mother&)在尝试编译代码时会出现此错误。

据我所知:由于 c 的类型是Child, Child::m 有一个类型参数,Child而 m 是Motherin类型,c.m(m)那么需要调用来自 Mother 类的函数 m() 。

class Mother{
 public: 
  void m(const Mother&) {
  }
};

class Child: public Mother{
 public:
  void m(const Child&) {
  }
};



int main() {
  Mother m;
  Child c;
  c.m(m);
}
Run Code Online (Sandbox Code Playgroud)

Jes*_*uhl 5

您的Child类没有m采用Mother. 它被 中m声明的隐藏Child。您可以通过添加取消隐藏using Mother::m;Child