Patrick Jane
Patrick Jane

Reputation: 53

Overloading the += operator

I have the following code to overload the + and += operators respectively for the class Date. The operator + was successfully overloaded, and it will take an integer n and increase Date objects by n days. This is done by applying the next_day function n times.

inline Date operator+(Date d, int n)
{
    for(char j=1; j<=n; j++){
        d=d.next_day(d);
    }
    return d;
}
inline Date operator+=(Date d, int n)
{
    Date p=d+n;
    return p;
}

Having overloaded the + operator, I'm using it to define the overloading of += as well. But although no errors occurred in compilation, when I use the overloaded += it doesn't seem to have any effect.

Here's my main.cpp:

#include <iostream>
#include "Date.h"
using namespace std;

int main() {

Date Initialday = Date (12,1,2012);

Initialday+=1;
cout <<"Next day = "<< Initialday <<endl;

return 0;
}

Running the main function still gives me 12/1/2012 instead of 12/2/2012. What am I doing wrong? Note: I've already overloaded << to output Date objects in a readable format, so I don't think that's the issue.

Upvotes: 3

Views: 9403

Answers (2)

juanchopanza
juanchopanza

Reputation: 227390

The main problem is that your += is creating a new Date object and returning it. That is the wrong semantics, plus you do not assign that return value to anything. A += operator should act on the instance it is applies to and return it by reference:

inline Date& operator+=(Date& d, int n) {
   return d = d + n;
}

Usually, this would be implemented as a member function, with + implemented in terms of +=:

class Date
{
 public:
    Date& operator+=(int n) {
       // perform whatever operation is required using
       // the state of the instance.
       return *this;
    }
};

inline Date operator+(Date lhs, int rhs) {
  return lhs += rhs; // calls member +=
}

The cleanest way would be to provide a time duration class, and implement all the operators in terms of a Date and a TimeDuration:

struct TimeDuration { .... };

class Date
{
 public:
  Date& operator+= (const TimeDuration& rhs) { .... }
};
inline Date operator+(Date lhs, const TimeDuration& rhs) { return lhs += rhs; }

Upvotes: 3

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

The simple fix is to take your Date object in by reference, modify it, and return it by reference. That's the expected behavior of operator+=.

inline Date& operator+=(Date &d, int n)
{
    d = d + n;
    return d;
}

However, to implement operator+= in terms of operator+ is backwards. It should be the other way around. operator+= should act on the members of the object, changing them directly. Then operator+ should be implemented in terms of that:

inline Date& operator+=(Date& lhs, int rhs)
{
    ... // code here to modify lhs directly

    return lhs;
}

inline Date operator+(Date lhs, int rhs)
{
    return lhs += rhs;
}

Upvotes: 5

Related Questions