6

I have a baseclass that has two functions of same name but with different signatures in a 2-level inheritance.

struct A {
    virtual void f(int) { }
    virtual void f(int, int) { };
    virtual void f1(int) { }
};

struct B: public A { };

struct C: public B {
  void f(int, int) { }
  void f1(int) { }
};

int main() {
 C obj;
 obj.f1(0);
 obj.f(0,0);

 obj.f(0);    // (1) cannot be found
 obj.B::f(0); // (2) works

}

I would have expected my compiler (gcc-4.3.2) to find the correct definition at (1), but I get

g++     main.cpp   -o main
main.cpp: In function 'int main()':
main.cpp:20: error: no matching function for call to 'C::f(int)'
main.cpp:10: note: candidates are: virtual void C::f(int, int)
distcc[2200] ERROR: compile main.cpp on localhost failed
make: *** [main] Error 1

(2) on the other hand works.

What do I need to fix to make (1) work in general?

1
  • 1
    +1 for well-written post with complete example Commented Jan 19, 2011 at 23:13

3 Answers 3

6

Write using A::f inside the definition of C.

You are a victim of name hiding! void C::f(int, int) hides void A::f(int), just because.

Sign up to request clarification or add additional context in comments.

Comments

5

The C++ name lookup rules have it so that if a name is redefined in one scope, all overloads of that name are hidden.

But you can use using to help. Like this:

class A {
    public:
    int f(int x) { cout << "A::f 1\n"; }
    int f(int x, int y) { cout << "A::f 2\n"; }
};

class B : public A {
    public:
    using A::f;
    int f(int x) { cout << "B::f 1\n"; }
};

int main()
{
    B b;

    b.f(27, 34);

    return 0;
}

Output is:

A::f 2

Comments

3

The short answer to "why" is "because that's how overloading works." You are hiding the f(int) overload in C. The longer answer is much longer.

You can un-hide it by doing this:

struct C: public B {
  using A::f;
  void f(int, int) { }
  void f1(int) { }
};

5 Comments

Can you tell us the longer answer?
@honk: Not right now, I'm short on time. But I'll enlist some help with this.
It's not long, but here's an explanation: stackoverflow.com/questions/888235/…
Thanks @Fred Larson, I should have found that.
@John: Never let answering get in the way of beer-o-clock, right? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.