I have the following method which returns the local declared Object by value:
Human Human::getLocalDeclaredHuman() {
Human human;
std::cout << &human << std::endl;
return human;
}
And I call this method:
Human a;
Human b = a.getLocalDeclaredHuman();
std::cout << &b << std::endl;
std::cout << b.getName() << std::endl;
and this is the output of the running program:
0x22fe58
0x22fe58
John Doe
So the variable human
which is declared local in the method has the same address as the variable b. I thought return-by-value
will create a copy of the object and that the object b has another address like the object human which is declared locally.
My question:
If here b and human have the same address, where is the difference between return-by-value and return-by-reference?