Client Login
Email:
Password:

Overloaded functions in C++

My teammate and I just spent a couple hours debugging an overloaded function:

We had a method foo( int num ) that was called passed a (char*):

foo( (char*)"test" );

...yet the integer version of the method was still being called, and quite happily at that! Huh!? It turned out to be the same issue noted here:

Here's the mess you're in: if Base declares a member function f(double x), and Derived declares a member function f(char c) (same name but different parameter types and/or constness), then the Base f(double x) is "hidden" rather than "overloaded" or "overridden" (even if the Base f(double x) is virtual).

Alas, my compiler doesn't like the noted "using" syntax, so I have to go about solving things the "long way" (either re-defining all the methods in my derived class, or renaming the derived methods so overloading no longer happens).

I can't believe I've never run into this "feature" of C++ before! And I can't believe this is how they chose to implement things! :)

It seems to me that if the method has a unique signature that's not redefined in the derived class, the compiler should happily look in the base class for that implementation. If I have foo(char) in my base class, but happen to have a redefined foo(int) in the derived class...why should it care? Assuming the data is always cast to the correct type, I fail to see why C++ requires we go through all this extra work.

If anyone can offer more insight, I'm very curious as to why C++ is wired this way.

--n

Comments
 
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner