Is there a way to set the default parameter as a function of the previous parameter? [C ++]

I suppose not, but I just wanted to check - is there a way in C ++ to do something like the following? Obviously, when I try to do the following, I get an error based on the area around the bar.

void foo(Bar bar, int test = bar.testInt) { ... } 
+4
source share
1 answer

If the test value is not valid, you may find that:

 void foo(Bar bar, int test = -1) { //assuming -1 is invalid if(test == -1) test = bar.testInt; //... } 

If not, you can always use overloaded functions:

 void foo(Bar bar, int test) { //... } void foo(Bar bar) { foo(bar, bar.testInt); } 
+7
source

All Articles