简体   繁体   中英

Functions with different value of default arguments

How to parse function arguments, when all have default values and only some of them are used? Example: I have a function with several arguments with default values

void fun(string a="", int b=0, string c="", int d=0)
{
 //parse used arguments somehow
}

I want to use it with different value of arguments, for example:

fun("foo", 10);
fun(10, 10);

How can I determine which arguments were used? Maximum value of arguments is known and order will be always the same. I do not want to run function like:

fun("", 3, "", 10);

And I cannot use variadic functions.

Any ideas?

Overload the function.

void fun(int b, int d) { fun("", b, "", d); }

Then fun(3, 10) becomes equivalent to fun("", 3, "", 10) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM