Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with this inheritance?

Tags:

c++

templates

I just don't get it. Tried on VC++ 2008 and G++ 4.3.2

#include <map>


class A : public std::multimap<int, bool>
{
public:
    size_type erase(int k, bool v)
    {
        return erase(k); // <- this fails; had to change to __super::erase(k)
    }
};

int main()
{
    A a;
    a.erase(0, false);
    a.erase(0); // <- fails. can't find base class' function?!

    return 0;
}
like image 618
Filip Frącz Avatar asked Nov 26 '22 20:11

Filip Frącz


1 Answers

When you declare a function in a class with the same name but different signature from a superclass, then the name resolution rules state that the compiler should stop looking for the function you are trying to call once it finds the first match. After finding the function by name, then it applies the overload resolution rules.

So what is happening is the compiler finds your implementation of erase(int, bool) when you call erase(0), and then decides that the arguments don't match.

like image 176
Greg Hewgill Avatar answered Nov 29 '22 10:11

Greg Hewgill