Basically I want to know in C++ if I call a function and pass the arguments by reference, then the arguments are modified only after the function returns or it is possible that they get changed exactly when they are modified inside the body before reaching to a return point?
I need to return some objects before deleting them at the end of a function. Obviously it is not possible to access the already deleted object when the function returns.
A reference can be seen as an alias to the actual object it's referencing so whenever you mutate an object via a reference, it's directly affecting the referenced object.
void foo(int& my_int) {
my_int = 10; // <- the change happens exactly here
// `an_int` declared in `main` is now 10
// do other things ...
}
int main() {
int an_int = 0;
foo(an_int);
}