$ cat test.cpp
#include <iostream>
#include <string>
class Super
{
public:
const char* s;
};
class Sub : public Super
{
};
void f(Super& a, Sub& b)
{
std::cout << "called with: " << a.s << ", " << b.s << std::endl;
}
void f(Sub& a, Super& b)
{
std::cout << "called with: " << a.s << ", " << b.s << std::endl;
}
void g(Super& a)
{
std::cout << "g(Super) is called" << std::endl;
}
void g(Sub& a)
{
std::cout << "g(Sub) is called" << std::endl;
}
int main(char ** argv, int argc)
{
Super sup; sup.s = "super";
Sub sub; sub.s = "sub";
f(sup, sub);
f(sub, sup);
f(sub, sub);
g(sup);
g(sub);
}
$ g++ test.cpp
test.cpp: In function 'int main(char**, int)':
test.cpp:39: error: call of overloaded 'f(Sub&, Sub&)' is ambiguous
test.cpp:14: note: candidates are: void f(Super&, Sub&)
test.cpp:18: note: void f(Sub&, Super&)
$ g++ test.cpp
$ ./a.out
called with: super, sub
called with: sub, super
g(Super) is called
g(Sub) is called
I am curious if anyone knows how C++ handles, resolves at compile time the overloading of functions of the same name but which take different arguments and what the shortcomings of whatever the C++ people did was. How does C++ determine what is the best match in cases when the arguments pattern match correctly against multiple signatures? I assume some method needs to exist to cope with unifying the argument types against the formal list and some scoring function would need to exist to assess which particular match is best.
Thanks in advance,
Silenus.