0

Can I choose which argument to write?

in foo(), I want to change the default value of c (third argument) without write a and b. example: int foo(int a=3, int b=5, int c=9) { return a+b+c;} int main() { std::cout << foo(); //print 17 std::cout << foo(c=0); // print 8 }

10th Oct 2017, 11:20 AM
Humberto da Silva Neto
Humberto da Silva Neto - avatar
2 odpowiedzi
+ 16
As you know, default parameters must be the right-most parameters. So, you can't do such thing (the compiler would think you are trying to pass in your c value for a). Nomeh pointed out to an overwrite in which after passing a and b by their default value again, your last parameter finally reaches its destination.
10th Oct 2017, 12:06 PM
Babak
Babak - avatar
+ 1
std::cout<<foo(3,5,0); will change the value of c from 9 to 0
10th Oct 2017, 11:51 AM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar