Understanding the 'this' Pointer in Operator Overloading

  • Thread starter chaoseverlasting
  • Start date
  • Tags
    Operator
In summary, the conversation discusses the use of the this pointer in operator overloading, specifically in the context of overloading the assignment operator (=). The language used in the explanation can be ambiguous, but it ultimately means that the function should return the object being assigned in order for the assignment chain to work. The object on the left-hand side of the = sign owns the call, meaning that the function should return that type of object. This is important to understand for multiple assignments as well.
  • #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:

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
  • #2
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::eek:perator=(const specialratio& r) and not specialratio& specialratio::eek:perator=(const ratio& r)
 
  • #3
Thank you. That really clears it up. It also makes the multiple assignment thing easier to understand.
 

FAQ: Understanding the 'this' Pointer in Operator Overloading

1. What is the purpose of the 'this' pointer in operator overloading?

The 'this' pointer in operator overloading allows for the use of the current object within the overloaded operator function. It helps to differentiate between the objects being used and ensures the correct object is being modified.

2. How is the 'this' pointer used in operator overloading?

The 'this' pointer is used by passing it as the first parameter to the overloaded operator function. This allows the function to access the current object and perform the desired operation on it.

3. Can the 'this' pointer be used in all types of operator overloading?

Yes, the 'this' pointer can be used in all types of operator overloading, including unary, binary, and friend functions. It is essential to use the 'this' pointer when overloading the assignment operator to avoid issues with self-assignment.

4. Can the 'this' pointer be used in non-member functions?

No, the 'this' pointer can only be used in member functions as it points to a specific object instance. Non-member functions do not have access to a specific object, so the 'this' pointer would not be relevant.

5. Are there any limitations to using the 'this' pointer in operator overloading?

One limitation of using the 'this' pointer in operator overloading is that it cannot be used in static member functions as they do not have access to a specific object instance. Additionally, the 'this' pointer can only be used in member functions of a class, not in global or namespace scope.

Similar threads

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