xcode - 为什么使用重载的虚拟函数给出'is hidden'警告

xcode - why is a warning of 'is hidden' given with overloaded virtual functions

本文关键字:is 警告 hidden 函数 为什么 重载 虚拟 xcode      更新时间:2023-10-16

我有一个基类,例如:

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

xcode给出了methodA被隐藏的警告-一切似乎都如我所期望的那样工作(从A派生的类可以通过A指针访问,并使用methodA的任何一个)。

我猜从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.
}
解决方案是在B中添加using声明:
class B : public A {
public:
  using A::methodA;  //bring all overloads of methodA into B's scope
  virtual void methodA(int) {}
};
相关文章: