Returning reference to object is not changing the address in c++
- by ashish-sangwan
I am trying to understand functions returning a reference. For that I have written a simple program:
#include<iostream>
using namespace std;
class test
{
int i;
friend test& func();
public:
test(int j){i=j;}
void show(){cout<<i<<endl;}
};
test& func()
{
test temp(10);
return temp; //// Address of temp=0xbfcb2874
}
int main()
{
test obj1(50); // Address of obj1=0xbfcb28a0
func()=obj1; <= Problem:The address of obj1 is not changing
obj1.show(); // // Address of obj1=0xbfcb28a0
return 0;
}
I ran the program using gdb and observed that the address of obj1 still remains same, but I expect it to get changed to 0xbfcb2874. I am not clear with the concept. Please help.