1

This following not compile with (GCC) 4.8.2. It complains like this:

error: no matching function for call to ‘classderivedderived::dump(int)’

#include <iostream>

class classbase{
public:
    void print() {std::cout << "base\n";}
    virtual void dump() = 0;
    void dump(int i) {std::cout << i << " blech\n";}
    int i;
};

class classderived : public classbase {
public:
    int ii;
};

class classderivedderived : public classderived {
public:
    void dump() {std::cout << "blah\n"; dump(10);}
    int iii;
};

int main() {
    classderivedderived yellow;
    yellow.i = 5;
    yellow.dump();
}

It does compile and run if

(1) dump(10) => classbase::dump(10)

OR

(2) dump(10) => puddledump(10) and void dump(int i) => void puddledump(int i)

Should this be happening? Why cannot gcc resolve the function call dump(10) on the basis of the argument type, as it would for any other function call? I thought that it would resolve that call because dump(int) is a member function of classderived by virtue of begin a member of classbase.

uchar
  • 2,552
  • 4
  • 29
  • 50
  • That's called "name hiding". For explanation about why it is this way, see [here](http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the). – jrok Jun 04 '14 at 20:25

1 Answers1

7

Yes, this is suppose to happen because your redeclaration of another function named dump() in classderivedderived is hiding the inherited dump(int). Having a different signature doesn't matter, it is hidden.

Bring it into scope with using :

class classderivedderived : public classderived 
{
  public:
    using classderived::dump;
    void dump() {std::cout << "blah\n"; dump(10);}
    int iii;
};
quantdev
  • 23,517
  • 5
  • 55
  • 88