user2148583
user2148583

Reputation: 45

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

I have a base class e.g.:

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

xcode gives out warnings of methodA was hidden - everything sems works as I'd expect (classes derived from A can be accessed via an A pointer and use either of the methodA's).

Upvotes: 1

Views: 2579

Answers (1)

I guess one of the classes derived from A (let's say it's B) only overrides one of the overloads of methodA(). In this case, the other overload of methodA is hidden in B. Example:

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

The solution is to add a using declaration into B:

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

Upvotes: 6

Related Questions