I am reading a C++ puzzle here: http://gotw.ca/gotw/005.htm
I did not understand his explanation on static vs dynamic overload resolution (or default paramaters), so I tried to distill the issue and write some tests myself:
class Base {
public:
virtual void foo() {cout << "base/no parameters" << endl;}
virtual void foo(int a) {cout << "base/int" << endl;}
};
class Derived : public Base {
public:
void foo() {cout << "derived/no parameters" << endl;}
void foo(double a) {cout << "derived/int" << endl;}
};
int main() {
Base* ptr = new Derived;
ptr->foo();
ptr->foo(1.0);
}
The output is:
derived/no parameters
base/int
How come in the call to foo()
, C++ seems to recognize that it is pointing to a Derived
but in the call foo(1.0)
, C++ does not see void foo(double a)
function in the Derived
class?
In my mind there are competing ideas, that C++ has polymorphism, which explains the first call, but that overload resolution is done at compile-time, which explains the second call.