Mahmoud Hosseinipour
Mahmoud Hosseinipour

Reputation: 293

overloading an operator in C++

I'm reading about operator overloading in C++,I have read many tutorial documents & Q&A but I can't find my answer. in the below code:

class Box
{
   public:
      // Overload + operator to add two Box objects.
      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;
      }
};

int main( )
{
   Box Box1;
   Box Box2;
   Box Box3;

   // Add two object as follows:
   Box3 = Box1 + Box2;

   return 0;
}

as you can see we override + operator for the BOX class.When we call override operator in main function which one of the class instances used as a argument (Box1 or Box2 ?) & which one is accessed by this ? actually which one called override operator?why we use & keyword in argument? WBR.

Upvotes: 0

Views: 2130

Answers (2)

Sahil Dupare
Sahil Dupare

Reputation: 11

Box operator+(const Box& b)

Here, the addition operator is used to add two Box objects and return final Box object. For that an object is passed as an argument whose properties will be accessed using another object. The object(Box1) which will call this operator(+) can be accessed using this operator, while the argument which is passed is Box2.

Upvotes: 0

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145204

The left hand operand is the this argument. To avoid this obscurity, and some more practically significant problems, simply define this operator as an ordinary (operator) function outside of the class. However, operators that modify, such as +=, are IMO best defined as class members.


Definition outside of class:

class Box
{
private:
    // Data members, unspecified in the question.

public:
    // Public interface.
};

// Overload + operator to add two Box objects.
auto operator+( Box const& a, Box const& b)
    -> Box
{
    Box result;
    result.length  = a.length + b.length;
    result.breadth = a.breadth + b.breadth;
    result.height  = a.height + b.height;
    return result;
}

If your data members are not public (you don't specify anything about it), then for the above out-of-class approach you need to add a friend declaration of the operator function, in the class. E.g.

friend auto operator+( Box const& a, Box const& b) -> Box;

An alternative with almost the same effect as an out-of-class definition, is to define the operator as a friend function within the class, like this:

class Box
{
private:
    // Data members, unspecified in the question.

public:
    // Public interface.

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

Here you don't need a separate friend declaration.

Another difference is that this in-class-defined friend function will only be found via Argument Dependent Lookup (ADL), which means that it's not easy to e.g. take its address. I don't know a way to do that. But at least knowing about this approach is necessary in order to understand certain idioms such as the Barton Nackman CRTP-based trick for endowing a class with relational operators.


Re

why we use & keyword in argument?

… that's just a convention that overall is an optimization, avoiding some needless data copying. For small types, on modern machines, it may however have the opposite effect.

Upvotes: 2

Related Questions