Understanding the 'this' Pointer in Operator Overloading

  • Thread starter Thread starter chaoseverlasting
  • Start date Start date
  • Tags Tags
    Operator
AI Thread Summary
The discussion centers on the use of the "this" pointer in operator overloading, specifically for the assignment operator in C++. The user expresses confusion about which object "owns the call" during an assignment operation. In the example provided, when assigning one object of the class `ratio` to another (e.g., `a = b`), the "this" pointer refers to the object on the left side of the assignment (in this case, `a`). This clarifies that the overloaded assignment operator should return a reference to the object that owns the call, which is the left-hand side object. The conversation also highlights that when dealing with derived classes, the appropriate operator overload is determined by the type of the left-hand side object, facilitating understanding of assignment chains and operator overloading in C++.
chaoseverlasting
Messages
1,050
Reaction score
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:

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?
 
Technology news on Phys.org
Inside the operator= function call the this pointer is a pointer to the object on the left-hand side of the = sign (so a in your example). The left-hand side is also the type of object that gets the call. I.e.

ratio a;
specialratio b;
a=b;

would call ratio& ratio::operator=(const specialratio& r) and not specialratio& specialratio::operator=(const ratio& r)
 
Thank you. That really clears it up. It also makes the multiple assignment thing easier to understand.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Replies
23
Views
2K
Replies
18
Views
3K
Replies
4
Views
2K
Replies
52
Views
4K
Replies
89
Views
6K
Replies
31
Views
3K
Replies
3
Views
2K
Replies
1
Views
8K
Back
Top