Progrev
Progrev

Reputation: 13

C++ methods with same name but different params

Why in C++ it was done so that the compiler "forgets" about other methods with the same name but different parameters if you overrides one of such methods?

struct A {
  void virtual f() {}
  void virtual f(int) {}
  void testA() {f(); f(1);} // OK
 };

struct B :  public A {
  void f() override {}
  void testB() {f(); f(1);} // Error
};

It seems that this is pointless behavior ... no?

Upvotes: 0

Views: 158

Answers (2)

Jojje
Jojje

Reputation: 381

But you could add a using-statement to make all the A::f methods visible, something similar to this:

  struct B :  public A {
    using A::f;
    void f() override {}
    void testB() {f(); f(1);} // No Error
  };

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409216

When you override a member function in a child-class, you hide the name of the parent class, it's just how the language works. If you want to use a function from the parent class you have to be explicit, like e.g. A::f(1).

Upvotes: 1

Related Questions