#include <iostream>
using namespace std;
struct Base
{
virtual int foo () {return 1;}
virtual int foo (int i) {return 2;}
virtual ~Base () {}
};
struct Derived : Base
{
virtual int foo (int i = 3) {return i;}
};
int main ()
{
Derived* d1 = new Derived;
Base* d2 = new Derived;
cout << d1->foo() << " " << d1->foo(4) << endl;
cout << d2->foo() << " " << d2->foo(4) << endl;
delete d2;
delete d1;
}
I was a little bit surprised to see the output of this short program:
3 4
1 4
Specifically, the '1' was unexpected. What rule (or rules) make it so
that d2->foo() calls Base::foo() rather than Derived::foo() with a
default argument?
Thanks,
Mark
using namespace std;
struct Base
{
virtual int foo () {return 1;}
virtual int foo (int i) {return 2;}
virtual ~Base () {}
};
struct Derived : Base
{
virtual int foo (int i = 3) {return i;}
};
int main ()
{
Derived* d1 = new Derived;
Base* d2 = new Derived;
cout << d1->foo() << " " << d1->foo(4) << endl;
cout << d2->foo() << " " << d2->foo(4) << endl;
delete d2;
delete d1;
}
I was a little bit surprised to see the output of this short program:
3 4
1 4
Specifically, the '1' was unexpected. What rule (or rules) make it so
that d2->foo() calls Base::foo() rather than Derived::foo() with a
default argument?
Thanks,
Mark
Comment