In the book "C++ complete reference", it is mentioned that "When an object is returned by a function, a temporary object is automatically created that holds the return value. It is this object that is actually returned by the function". But when I write code and tested, the address of object inside the function and address of the temporary objected returned are coming out to be same. Is anything changed in new compilers regarding returning objects?? If not, somebody please help me to understand why it is so. Thanks
class obj
{
public:
obj()
{
cout<<"constructor\n";
}
~obj()
{
cout<<"destructor\n";
}
};
obj getObj()
{
obj s;
cout<<"In getObj:"<<&s<<"\n";
return s;
}
void function(const obj &s)
{
cout<<"address returned by getObj:"<<&s<<"\n";
}
int main()
{
function(getObj());
return 0;
}
Output: constructor In getObj:0x7ffcf0bdc4c7 address returned by getObj:0x7ffcf0bdc4c7 destructor