Hi Guys...
I have some basic doubts with references and pointers and need some understanding on the behavior of this code
#include <iostream>
using namespace std;
class a
{
public:
int getAmount(a&);
private:
int amount;
};
int
a::getAmount(a& p)
{
cout << this << endl;
cout << p << endl;
//cout << &p << endl;
return amount;
}
int main()
{
a *obj = new a();
obj->getAmount(*obj);
//a obj1;
//obj1.getAmount(obj1);
};
When i'm passing *obj to getAmount, i realize i'm passing the object to it, and the parameter is a&, so my understanding is that 'p' now should be my reference to the object and cout << p, should work. But i get compilation error that operator << is not overloaded for type 'a'(which means that 'p' is the actual object itself). instead cout << &p works and gives me the address. Why is that so?
If from main i use the commented code then both cout << p and cout << &p work the same and give me the address of the object in memory. Thus here 'p' works as a reference to the object.