This question refers to common problems discussed in these questions:
Can virtual functions have default parameters?
Virtual functions default parameters
Here is what currently happens in c++ with default parameters to virtual functions:
struct Base
{
virtual void foo(int one = 1, int two = 2)
{ cout << "one: " << one << " two: " << two << endl; }
};
struct Derived : public Base
{
virtual void foo(int one = 3, int two = 4)
{ Base::foo(one, two); cout << " derived!" << endl; }
};
int main()
{
Base* b = new Base();
Base* d = new Derived();
Derived* dp = new Derived();
b->foo();
d->foo();
dp->foo();
return 0;
}
output:
one: 1 two: 2
one: 1 two: 2
derived!
one: 3 two: 4
derived!
This is the behaviour I wish existed in c++ virtual function default parameters:
#include <iostream>
using namespace std;
struct Base
{
virtual void foo () { foo(1, 2); }
virtual void foo (int one) { foo(one, 2); }
virtual void foo(int one, int two)
{ cout << "one: " << one << " two: " << two << endl; }
};
struct Derived : public Base
{
virtual void foo() { foo(3, 4); }
virtual void foo(int one, int two)
{ Base::foo(one, two); cout << " derived!" << endl; }
};
int main()
{
Base* b = new Base();
Base* d = new Derived();
Derived* dp = new Derived();
b->foo();
d->foo();
dp->foo();
return 0;
}
output:
one: 1 two: 2
one: 3 two: 4
derived!
one: 3 two: 4
derived!
So, basically, if I want to override a default parameter in a parent class, I will just create a new foo with that number of arguments. Note that the derived overrides the no-arg condition, but not the one-arg condition. As a side note, my current project uses a purely virtual base class. I often have pointers of the base class type and of the derived class type. I want a call from either pointer to have the same result.
QUESTIONS:
I have read many questions related to this subject, but they all don't seem to give reasonable solutions. Most of the solutions would result in uglier code all throughout your project.
Some say "Don't use default parameters on virtual functions," but then I would need to put the default in every place I call the function. It seems it would be better to just put a comment that says, "also change base class" and "also change derived class" than to have to change everywhere the function is called.
Some say to only have the default parameters in the base class, but that means that any pointer to a derived object would need to be cast back to a base pointer before the defaults could be used. That makes a lot of code ugly as well.
Are there reasons I should avoid the above design?