header.h
#include <iostream>
using namespace std;
class A
{
public:
virtual void display(int i=5) { cout<< "Base::" << i << endl; }
};
class B : public A
{
public:
void display(int i=9) { cout<< "Derived::" << i << endl; }
};
source.h
#include <iostream>
#include "header.h"
using namespace std;
int main()
{
A * a = new B();
a->display();
A* aa = new A();
aa->display();
B* bb = new B();
bb->display();
}
output
Derived::5
Base::5
Derived::9
My understanding was default parameter functions were resolved during compile time using function overloading. Virtual functions were then resolved during runtime using function overriding.
But what is happening is a mess.
How does the function resolution actually happen here?
Default argument resolution is based on the static type of the object through which you call the function
(i.e. based on the pointer type). social.msdn.microsoft.com/Forums/en-US/… – Logicianvirtual void display() { display(9); }
and in Derivedvoid display() { display(5); }
– Trenatrenail