Understanding the 'this' Pointer in Operator Overloading

  • Thread starter Thread starter chaoseverlasting
  • Start date Start date
  • Tags Tags
    Operator
Click For Summary
SUMMARY

The discussion clarifies the use of the 'this' pointer in operator overloading within C++. Specifically, when overloading the assignment operator (=) in the class 'ratio', the function must return a reference to the object that owns the call, which is indicated by the 'this' pointer. In the example provided, when executing 'a = b;', the 'this' pointer refers to 'a', the object on the left-hand side of the assignment. This understanding simplifies the concept of multiple assignments in C++.

PREREQUISITES
  • Understanding of C++ class structures
  • Familiarity with operator overloading in C++
  • Knowledge of pointers and references in C++
  • Basic concepts of assignment operations in programming
NEXT STEPS
  • Study C++ operator overloading techniques in detail
  • Learn about the 'this' pointer and its implications in C++
  • Explore advanced C++ concepts such as copy constructors and assignment operators
  • Investigate the behavior of multiple assignments in C++
USEFUL FOR

C++ developers, software engineers, and students learning about operator overloading and memory management in C++. This discussion is particularly beneficial for those looking to deepen their understanding of class member functions and the 'this' pointer.

chaoseverlasting
Messages
1,051
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.
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
Replies
53
Views
5K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 52 ·
2
Replies
52
Views
4K
Replies
10
Views
2K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 3 ·
Replies
3
Views
3K