No, this isn't about the mistake everyone makes of passing locals. I'm just trying to understanding returning a reference to an object you pass in (I'm reading through primer).
So if I have a function like this:
const foo & foo::function2(const foo & val) const
{
using namespace std;
return *this;
}
and then I'm in main doing this:
foo object1;
object1.someproperty = 7;
foo object2 = object1.function2(object1);
object2.someproperty = 5;
cout << &object1 << endl;
cout << &object2 << endl;
When I return by reference, shouldn't object2 have the same address (and properties) as object1? Shouldn't changing "someproperty" in one object alter the value in the other? Or, does returning a reference to an object simply copy over the values into the new object?
It seems like the same thing is going on as if I just said that I was going to return a foo object instead of a reference to one.