I am bit surprised with this code snippet! the caller is passing value, but how is the function handling its address ?
void increase1(int &value) {
value++;
}
int main() {
int number = 5;
increase1(number);
}
It would be helpful if someone explains in detail. Thank you.
Because
increase1
takes a reference to anint
, the caller sends the value by reference automatically.Sending a value by reference functions the same as sending a pointer, except that
If the reference is coming from a pointer, check to make sure the pointer isn't null before converting it into a reference. If you already know the pointer isn't null, then you don't have to check (of course).