xcode - 为什么在重载的虚函数中给出'隐藏'的警告

use*_*583 1 c++ virtual hidden warnings

我有一个基类,例如:

class A {
public:
  virtual void methodA(int) {}
  virtual void methodA(int, int, int) {}
};
Run Code Online (Sandbox Code Playgroud)

xcode发出了隐藏方法A的警告 - 所有sems都按照我的预期工作(从A派生的类可以通过A指针访问并使用方法A的任何一个).

Rei*_*ica 6

我想其中一个派生的A(让我们说它的B)只会覆盖其中一个重载methodA().在这种情况下,其他的过载methodA隐藏B.例:

class A {
public:
  virtual void methodA(int) {}
  virtual void methodA(int, int, int) {}
};

class B : public A {
public:
  virtual void methodA(int) {}
};

int main()
{
  A a;
  B b;
  A *pa = &b;
  a.methodA(7); //OK
  a.methodA(7, 7, 7); //OK
  pa->methodA(7);  //OK, calls B's implementation
  pa->methodA(7, 7, 7);  //OK, calls A's implementation
  b.methodA(7); //OK
  b.methodA(7, 7, 7);  //compile error - B's methodA only accepts one int, not three.
}
Run Code Online (Sandbox Code Playgroud)

解决方案是将using声明添加到B:

class B : public A {
public:
  using A::methodA;  //bring all overloads of methodA into B's scope
  virtual void methodA(int) {}
};
Run Code Online (Sandbox Code Playgroud)