Overriding + varying default argument presence

I can variate argument default value presence
from base to derivative and have correctly usabe, overriding
functions (virtual). But should it always work?

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

struct base
{
   virtual void f(int i = 1){cout << "base::f(" << i << ")" << endl;}
};

struct deri: public base
{
   virtual void f(int i){cout << "deri::f(" << i << ")" << endl;}
};

int main()
{
   base b;
   b.f(0); // base::f(0)
   b.f(); // base::f(1)

   deri d;
   d.f(2); // deri::f(2)
   //d.f(); // error: no such function!

   base& br = d;
   br.f(); // deri::f(1)
   br.f(3); // deri::f(3)

   return 0;
}
Thats because, the virutal table does not maintain information about the default parameters...

The virtual table just stores the address of the function to be invoked.. i.e the address of the overrided function and it stores nothing else...

Topic archived. No new replies allowed.