Overloaded operators

Hi, im reading Cplusplus tutorials and i dont fully understand why does the new definition of operator+ for adding vectors needs only one parameter and not two (one per vector) and why does it need not the parameter itself but const CVector&
Also i dont understand the difference between &CVector and CVector&.
Sorry for my english.
Thanks!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// overloading operators example
#include <iostream>
using namespace std;

class CVector {
  public:
    int x,y;
    CVector () {};
    CVector (int a,int b) : x(a), y(b) {}
    CVector operator + (const CVector&);
};

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

int main () {
  CVector foo (3,1);
  CVector bar (1,2);
  CVector result;
  result = foo + bar;
  cout << result.x << ',' << result.y << '\n';
  return 0;
}
Last edited on
One argument is hidden due to how classes work. If you write:
1
2
CVector a(1, 2), b(2, 3), c;
c = a + b; // equal to c = a.operator+(b), you call operator+() function for object a, so you don't give it directly 

also, CVector& is reference to CVector type, and:
1
2
CVector a;
&a; // returns you memory adress of a object 

You cannot write &CVector
Last edited on
So when "redefining" operator+() inside the class you need to specify the type of the argument?
If that is the case, I dont understand why you need to declare it constant and why you have to include the & at the end (CVector is a type by itself).
Thanks for your previous response.
Last edited on
So when "redefining" operator+() inside the class you need to specify the type of the argument?

Yes, you specify the type of the variable on the right hand side of the "+" operator. You could also define CVector::operator+(double d); This would let you do something like vec1 = vec2 + 8.4;

If that is the case, I don't understand why you need to declare it constant

Since the + operator doesn't modify the right hand side object, it's good to define it that way so you can call it with a const object. It can also help the optimizer. In fact, you should define the operator as const also:
CVector operator + (const CVector&) const;
That way you'll be able to do vec1 + vec2 + vec3; and why you have to include the & at the end
This is for efficiency. If the method was operator+(const CVector param) then every time you called +, it would make a copy of the vector, use it as the param, and then destroy it. This isn't too big a deal with CVector, but imagine if it had 1000 elements instead: all that copying would be a performance killer.

Now some points about style, which you should feel free to ignore. When overriding a binary operator, I universally call the parameter "rhs" for "right hand side." It helps me remember that this is the right hand side of the + operation.

Also, if I need to write operator+(), I usually write operator+=() instead. Then you can get operator+() practically for free:
1
2
3
4
5
MyClass operator +(const MyClass &rhs) const {
    MyClass result(*this);
    result += rhs;
    return result;
}


Two useful operations for the price of one!
Thanks! your explanation help me a lot.
Topic archived. No new replies allowed.