1

我有一个基类,例如:

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

xcode 发出隐藏方法A 的警告-一切sems 都按我的预期工作(从A 派生的类可以通过A 指针访问并使用其中一个方法A)。

4

1 回答 1

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.
}

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

class B : public A {
public:
  using A::methodA;  //bring all overloads of methodA into B's scope
  virtual void methodA(int) {}
};
于 2013-03-08T13:52:35.697 回答