9

We have following class. I need explanation of some parts of code.

class CPoint3D
    {
    public:
      double x, y, z;

      CPoint3D (double dX = 0.0, double dY = 0.0, double dZ = 0.0) 
              : x(dX), y(dY), z(dZ) {}
      //what means these lines of    code?
      CPoint3D operator + (const CPoint3D& point) const;
      CPoint3D operator - (const CPoint3D& point) const;
      CPoint3D operator * (double dFactor) const;
      CPoint3D operator / (double dFactor) const;
};

I guess using

CPoint3D operator + (const CPoint3D& point) const;

function I can easily add/subtract/multiply/divide instances of CPoint3D class?

Can someone explain this with examples ? Thanks!

P0W
  • 46,614
  • 9
  • 72
  • 119
Nurlan
  • 2,860
  • 19
  • 47
  • 64

2 Answers2

13

There are millions of examples and/or articles of this on the web (including this one) so I won't re-iterate them here.

Suffice to say that when you add together two CPoint3D objects with obj1 + obj2, the function that gets called is operator+ for that class, with one object being this and the other being point.

Your code is responsible for creating another object containing the addition of those two, then returning it.

Ditto for the subtraction. The multiplicative operators are slightly different since they use a double as the other argument - presumably while it makes some sense to add/subtract individual members of your class for the additive operators, that's not as useful for the multiplicative ones.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
4

You may read some literature regarding C++ operator overloading. Also here, or here, or just Google it :)

Here is a simple example from cplusplus.com:

// vectors: overloading operators example
#include <iostream>
using namespace std;

class CVector {
  public:
    int x,y;
    CVector () {};
    CVector (int,int);
    CVector operator + (CVector);
};

CVector::CVector (int a, int b) {
  x = a;
  y = b;
}

CVector CVector::operator+ (CVector param) {
  CVector temp;
  temp.x = x + param.x;
  temp.y = y + param.y;
  return (temp);
}

int main () {
  CVector a (3,1);
  CVector b (1,2);
  CVector c;
  c = a + b;
  cout << c.x << "," << c.y;
  return 0;
}
Community
  • 1
  • 1
Didac Perez Parera
  • 3,734
  • 3
  • 52
  • 87
  • 3
    Maybe take const CPoint3D& point for the operator + as the OP does? – doctorlove Aug 16 '13 at 09:06
  • Well, the types of the arguments of overloaded non-assignment operators don't actually matter at all. They don't have to make any sense! This is not like copy/move constructors/assignment operators, where the argument must be one of the defined kinds of of _lvalue_ representing the same type so as to be considered a 'real' copy/move. But for any larger object, passing by `const &` reference would be more efficient. The example here, containing 2 basic types, I _think_ is right on the border where careful benchmarking would be required to prove whether passing by-copy or -ref is more efficient. – underscore_d Aug 07 '16 at 16:25
  • Btw, since the argument is by-value, if anyone wonders why this answer didn't just add to and return _that_, a good reason - which I presume Didac had in mind! - is that arguments are exempt from named return value optimisation: http://stackoverflow.com/q/6009278/2757035 So whereas you might think it's slow to create a temporary and add to it instead - doing so enables the compiler to (perhaps!) perform the large transformations inherent in RVO and obtain a much more efficient result by constructing `temp` directly at the call site, instead of literally returning a value that has to be copied. – underscore_d Aug 07 '16 at 16:34