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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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