0

I wondered if it's possible to default initialize a int& parameter other than this:

int temp;

void func(int& value = temp){
}

I mean i can initialize int* like this:

void func(int* value = NULL){
}

so i wondered if there is something similar for int& that doesn't requires me to create a temporary variable. I need to initialize it with something like NULL to check in the function if a variable was passed in and then change it. I want to do something like this:

int getSeed(int* seed = NULL){
if(seed != NULL)
*seed = *seed / 2; // some math here
return *seed;
}

with &seed as parameter. Thanks in advance!

SupaDupa
  • 161
  • 2
  • 14
  • You can utilize `optional` wrapper of some sort. – user7860670 Dec 01 '17 at 12:13
  • needing a parameter that can be `NULL` is where pointers are better suited than references, why do you still want to use a reference? – 463035818_is_not_an_ai Dec 01 '17 at 12:13
  • @tobi303 it's stupid, but i don't want to write & everytime i use the function, like func(&value); – SupaDupa Dec 01 '17 at 12:19
  • thats of course purely a matter of taste, but some argue that having to write the `&` is better because then at the call-site it is obvious whether it is pass-by-value or pass-by-reference. Iirc the google style guide even recommends always using pointers when passing by reference for exactly this reason. However, I am with you and prefer references... – 463035818_is_not_an_ai Dec 01 '17 at 12:22

0 Answers0