Reputation: 1152
I think, there is a common situation, when one function (a) is being called from within another one (b), while 'a' have some default parameters and 'b' is needed to support them. For example:
void a(int v1, int v2=0, int v3=1);
void b(int m1, int m2, int v1, int v2=0, int v3=1) {
// ...
a(v1, v2, v3);
// ...
}
But this is the violation of the DRY principle. It can leads a subtile bug, when default parameter of 'a' was changed, but not changed in 'b':
void a(int v1, int v2, int v3=0);
void b(int m1, int m2, int v1, int v2=0, int v3=1) {
// ...
a(v1, v2, v3);
// ...
}
Why there is no mechanism in C++ to inherit default parameter values? It might look like:
void a(int v1, int v2=0, int v3=1);
void b(int m1, int m2, int v1, int v2=default(a::v2, 0), int v3=default(a::v3, 1)) {
// ...
a(v1, v2, v3);
// ...
}
Whether there are languages, that have such syntax?
It might be an offtopic on this board?
Upvotes: 2
Views: 118
Reputation: 25936
"But this is the violation of the DRY principle. It can leads a subtile bug, when default parameter of 'a' was changed, but not changed in 'b'"
Frankly, I'd say that if you get this "bug", then your functions are too tightly-coupled. Standalone functions are supposed to, well, stand alone. If changing a default parameter for one function causes another function to break, I think you have a design problem there.
Upvotes: 0
Reputation: 70492
The issue is actually the use of magic numbers. If you remove the magic numbers, the problem is resolved nicely.
enum DefaultA { DefaultA_v2 = 0, DefaultA_v3 = 1 };
void a(int v1, int v2=DefaultA_v2, int v3=DefaultA_v3);
void b(int m1, int m2, int v1, int v2=DefaultA_v2, int v3=DefaultA_v3) {
// ...
a(v1, v2, v3);
// ...
}
Upvotes: 5