-4

which are the differences between those 3 implementations?

Box Box::operator+(const Box& b)
            {
                Box box;
                box.length = length + b.length;
                box.breadth = breadth + b.breadth;
                box.height = height + b.height;
                return box;
            }

or:

Box Box::operator+(const Box& b)
    {
        Box box;
        box.length = box.length + b.length;
        box.breadth = box.breadth + b.breadth;
        box.height = box.height + b.height;
        return box;
    }

or:

 Box Box::operator+(const Box& b)
    {
        Box box;
        box.length = this->length + b.length;
        box.breadth = this->breadth + b.breadth;
        box.height = this->height + b.height;
        return box;
    }

I noticed that with the second implementation, results are different wrt the other two implementations. In the first case i suppose that this-> is implicit. In the third case this is always the object who call that function, but the second case is equal to the others?

Many thanks!

Would be another possible solution to use:

example example::operator+(const example& obj2)
{
    example tmp_obj = *this;
    tmp_obj.a = tmp_obj.a + obj2.a;
    tmp_obj.b = tmp_obj.b + obj2.b;
    return tmp_obj;
}
rollotommasi
  • 461
  • 1
  • 6
  • 11

3 Answers3

4

Method 1

For implementation one, you are doing the method that I would personally recommend. You are adding the current object's properties with b's properties.

Method 2

Method B is just plain wrong:

Box operator+(const Box& b)
    {
        Box box;
        box.length = box.length + b.length;
        box.breadth = box.breadth + b.breadth;
        box.height = box.height + b.height;
        return box;
    }

box is not initialized, and it definitely is not the current object, so the values that you are adding won't make sense, since you are adding the default constructed values for the box object, not the current object instance's value. In fact, to give you a better idea of why Method 2 is wrong, it is the equivalent of:

Box operator+(const Box& b)
    {
        Box box;
        box.length += b.length;
        box.breadth += b.breadth;
        box.height += b.height;
        return box;
    }

Method 3

Method 3 is just an explicit way of doing method 1, with this-> meaning "refer to the current object". As you said, this-> is implied in method 1.

Box operator+(const Box& b)
            {
                Box box;
                box.length = length + b.length; //length == this->length
                box.breadth = breadth + b.breadth; //breadth == this->breadth
                box.height = height + b.height; //height == this->height
                return box;
            }
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • 2
    I would actual recommend they implement `operator+` with `operator+=` like `inline X operator+(X lhs, const X& rhs){ lhs += rhs; return lhs; }`so the addition code only has to sit in one place. – NathanOliver Sep 09 '16 at 14:37
  • Good point, but in the OP's case of Method 2, that is just wrong – Arnav Borborah Sep 09 '16 at 14:38
  • 1
    No doubt about that. – NathanOliver Sep 09 '16 at 14:40
  • 1
    @juanchopanza Ah that it would. Then I guess it would be better to make a copy in the function body and hope NRVO kicks in. – NathanOliver Sep 09 '16 at 15:08
  • Would be another good solution using: `example example::operator+(const example& obj2) { example tmp_obj = *this; tmp_obj.a = tmp_obj.a + obj2.a; tmp_obj.b = tmp_obj.b + obj2.b; return tmp_obj; }` – rollotommasi Sep 09 '16 at 15:35
  • Operator `+=` would be useful there @rollotommasi – Arnav Borborah Sep 09 '16 at 15:36
  • Total aside but if you implement `operator+` you almost certainly want it to be a `const` method. Otherwise you should think about implementing `+=` or whether the function really conforms to `+`. – Persixty Nov 04 '16 at 07:10
1

The reason the second one is different is because you are referencing the newly created Box in the second one with box.length, box.breadth, and box.height instead of the one preceding the function. Instead of taking in the values of the one preceding the +, it will be taking in the values of the newly constructed Box.

D. Law.
  • 234
  • 1
  • 3
  • 8
1

1) and 3) are essentially the same thing: the use of this-> makes sense only if you have to distinguish between members and local variables of same name - definitively not the case here.

2) is just plainly wrong: it defines the uninitialized local box in term of itself. If Box has a default constructor the result is well defined (but it is not the arithmetic sum of *this and b). If it doesn't, the result is essentially undefined (and hence compiler, optimization and running dependent)

Note that, in any case, the proper signature should be

Box operator+(const Box& b) const

Otherwise you won't be able to sum with temporaries (it happens withing long expressions)

Considering the symmetric nature of +, the best implementation should probably be

friend Box operator+(const Box& a, const Box& b)
{
    Box c;
    c.length = a.length + b.length;
    c.breadth = a.breadth + b.breadth;
    c.height = a.height + b.height;
    return c;
}

since it treats both arguments at same level.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63