- #1
chaoseverlasting
- 1,050
- 3
I've got a doubt regarding the this pointer. I am using schaums outlines programming with c++. In operator overloading (Chapter 11 p257), when you overload the assignment operator (=), the code looks something like this:
The problem is that I find the language ambiguous. It says:
'The return type is a reference to an object of the same class, but then this means that the function should return the object that is being assigned in order for the assignment chain to work, so when the assignment operator is being overloaded as a class member function, it should return the object that owns the call.'
Which object owns the call? If you have say,
ratio a,b;
a=b;
does a own the call or does b?
Code:
class ratio
{ public:
//constructor and copy constructor definition
ratio& operator=(const ratio&); //overloading assignment operator
private:
int num, den;
//other stuff
};
ratio& ratio::operator=(const ratio& r)
{
num=r.num;
den=r.den;
return *this;
}
The problem is that I find the language ambiguous. It says:
'The return type is a reference to an object of the same class, but then this means that the function should return the object that is being assigned in order for the assignment chain to work, so when the assignment operator is being overloaded as a class member function, it should return the object that owns the call.'
Which object owns the call? If you have say,
ratio a,b;
a=b;
does a own the call or does b?