Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

optional argument value

Tags:

c++

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 ?

like image 714
user1482030 Avatar asked Dec 19 '22 05:12

user1482030


2 Answers

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

inline void function(int a)
{
    function(a, a);
}
like image 70
Ralf Schallenberg Avatar answered Jan 01 '23 18:01

Ralf Schallenberg


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.

like image 29
thinkerou Avatar answered Jan 01 '23 19:01

thinkerou