int foo(vector<baz> & rick)
{
int num;
vector<baz> local;
//process and add stuff to local
//process num
rick = local;
return num;
}
The vector local has been created inside the function, and we are assigning the reference rick from it.
When the function returns, isnt the vector local deallocated?
But rick is a reference variable, my understanding of reference variables that if
a reference variable is assigned an object, no copying of objects occur, only that the reference variable (here rick) is now an alias for the object (here local). What did I miss?
The object that the reference is referring to is decided when the reference is created and can't be changed later. Any action on the reference affects the original object.
1 2 3 4 5
int a = 0;
int b = 1;
int& r = a;
r = b;
// r is still referring to a and a is now equal to 1