我有一个基类,例如:
class A {
public:
virtual void methodA(int) {}
virtual void methodA(int, int, int) {}
};
xcode 发出 methodA 被隐藏的警告 - 所有 sems 都按我预期的那样工作(从 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) {}
};
关于c++ - xcode - 为什么 'is hidden' 给出重载虚函数的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15295317/