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 }
2 Antworten
+ 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.
+ 1
std::cout<<foo(3,5,0); will change the value of c from 9 to 0