user258367
user258367

Reputation: 3407

default argument function signature change

I have a function whose current signature is f(a,b=0). I want to add another argument c. I want my function in such a way so that I can call f(a,b) which is currently the behavior and f(a,c). One way is to overload the function and duplicate the function code. I do not want to call f(a,b) from f(a,c). I am working in C++.

Is there any standard design pattern or a solution which can avoid this code duplication ?

Upvotes: 0

Views: 1339

Answers (1)

David Schwartz
David Schwartz

Reputation: 182829

I'm not completely sure I follow you, but you can use variations on this:

void f(int a, int b=0, int c=0);
void f2(int a, int c=0) { f(a, 0, c); }

Notice that no code is duplicated. And now you have another function that calls your first function but allows you to specify the third argument rather than the second.

Upvotes: 6

Related Questions