I was trying to understand how a objects are stored in memory (heap) and how reference variable works. I wrote a simple program to understand the concept. But Why I am getting dereferenced address i.e. *(&cat) but not the &cat when function callBar() returns the address?
#include<iostream>
using std::cout;
using std::string;
class Cat{
public: string name;
};
Cat* callBar(Cat *cat){
cat = new Cat();
cout<<&cat<<"\n";
cout<<*(&cat)<<"\n";
return cat;
}
int main(void){
Cat *c = new Cat();
cout<<&c<<"\n";
cout<<callBar(c)<<"\n";
cout<<*(&c)<<"\n";
return 0;
}
The output of the code is:
0x7ffd38985d70
0x7ffd38985d48
0x56368fbd22b0
0x56368fbd22b0
0x56368fbd1e70
I created the instance of the class in the main and printed its address. I passed this object to another function and then printed its address again, which gave me new reference. Atlast, I deferenced it.
It is still not clear to me why I am getting dereferenced address i.e. *(&cat) but not the &cat when function returns?