2

I am trying to overload operators in C++. For the purpose, I wrote the following code:

#include <iostream>

using namespace std;

class Box
{
public: 
    int height;
    int width;
    int length;
public:
    Box operator+(const Box& b)
    {
        this->length = this->length + b.length;
        this->width = this->width + b.width;
        this->height = this->height + b.height;

        return *this;
    } 


};




int main()
{
    Box b1,b2;

    b1.length = 5;
    b1.width = 5;
    b1.height = 5;

    cout << "Length is : " << b1.length;
    cout << "width is : " << b1.width;
    cout << "height is : " << b1.height;

    b2.length = 5;
    b2.width = 5;
    b2.height = 5;

    b1 = b1 + b2 ;

    cout << "Length is : " << b1.length;
    cout << "width is : " << b1.width;
    cout << "height is : " << b1.height;


    cout << "Hello from first c++";
    return 0;
}

The main part is:

Box operator+(const Box& b)
        {
            this->length = this->length + b.length;
            this->width = this->width + b.width;
            this->height = this->height + b.height;

            return *this;
        } 

I can't understand:

  1. this->length = this->length + b.length;

  2. return *this;

this.length is not working here. why should I return *this? Is return this is not enough here?

learner
  • 4,614
  • 7
  • 54
  • 98

4 Answers4

4
  1. "this" is a pointer
  2. "this" pointer is a constant pointer that holds the memory address of the current object

Member signature:

Box operator+(const Box& b)

why should I return *this? Is return this not enough here?

If you returned this, then you would be returning a pointer Box*, and your member signature does not agree with that.

Hence, you need to return by value and hence, dereference it and return.

Étienne
  • 4,773
  • 2
  • 33
  • 58
basav
  • 1,475
  • 12
  • 20
1

(Setting aside the fact that your operator+() changes the value of this) the reason you return *this is to allow expressions like that:

Box a, b, c;
// ... init the Boxes
Box d = a + b + c;

In this case the result of a+b needs to be "fed" into operator+ to add the value of c into it. This is done by creating a new temp object that represents the result of a+b (in your case - this is what is returned as *this in your operator+).

Now you should see, that if you were to return a pointer this then there would be no way to elegantly write down the a+b+c as the result of a+b would no longer be a reference and in order to properly call the operator+ on it one would write something ugly like:

(a+b).operator+(c)

Same reasoning goes for the assignment operator= in the above expression - you really do want to return a reference to your object, not a pointer to it.

YePhIcK
  • 5,816
  • 2
  • 27
  • 52
0

You want to return *this whenever you return a reference or copy, and this only if you return a pointer to the class object (which you almost never will).

Davislor
  • 14,674
  • 2
  • 34
  • 49
0

"this" is a pointer to an object of type Box. Your return type is a concrete object of type Box.

When you return *this, you are dereferencing the pointer and returning a reference to your object (Box&) which then your compiler converts to a new object using the copy constructor Box(const Box&).

In fact if you wrote b3 = b1+b2 you would notice that modifying b3 would not modify b1 further. This concerning the letter of your question.

As a side note, though, you are making a bit of confusion in the way you are defining your operator. Usually when you overload your arithmetic operator you can overload both the operator+ and the operator+=

operator+ returns a new concrete object of the given type and is not supposed to modify the operand. It's typical signature is:

Box Box::operator+(const Box& R) const

So the typical way to implement it is to make a copy of one of the two operands and then do the sum and return this new object. Notice that the function is declared constant

If you want to modify the first operand the way it seems you want to do, you use the += operator. This operator has a different signature and it assumes you modify the operand and in this case it actually returns a reference which is supposed to be a reference to the left operand. So in this case you will have

Box& Box::operator+=(const Box& R)

And in this case you can see that the function is not constant because you assume you want to modify your operand and in this case you should return *this which is a reference to the object you just modified.

Quick guide on operator overloading

Triskeldeian
  • 590
  • 3
  • 18