user1482030
user1482030

Reputation: 827

optional argument value

i want to create a function with an optional argument which take the value of another argument. In the declaration of the function the following doesn't work, but this is exactly what i want to do :

void function(int a, int b=a)   //error

I try to set the default value of the variable b to the value of a. What is the cleanest way to do that ? Can we do that without changing the signature of the function ?

Upvotes: 4

Views: 161

Answers (2)

Ralf Schallenberg
Ralf Schallenberg

Reputation: 146

Write an additional function that takes just one argument and calls the original function:

inline void function(int a)
{
    function(a, a);
}

Upvotes: 10

thinkerou
thinkerou

Reputation: 1877

Default argument must be global variable or global constant, even function. But it can not be local variable, because local var can not ensure in build.

Upvotes: 1

Related Questions