previous | start | next

Non-member operator+ - where?

This non-member operator+ logically belongs with the Pair class. A class is a unit that keeps the related member functions of the class declared in one place (the Pair.h file) and defined in one place (the Pair.cpp file).

But where can we put the non-member operator+

We could declare it in the Pair.h file, but outside the class declaration:

class Pair
{
  int x, y;
public:
  Pair();
  Pair(int xval, int yval);

  int getX();
  int getY();
  string toString();
  Pair add(const Pair& other);
  Pair add(int d);

  Pair operator+(const Pair& other);
  Pair operator+(int d);
  
};
Pair operator+(int d, const Pair& p);

and write the implementation in the Pair.cpp file



previous | start | next