Overloaded Class Operator

Okay, so the code below is a copy of code from an online course thingy I'm attempting, and is meant to be demonstrating overloading an operator. In this case it's the + operator. However, I'm having difficulty following how it works.

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
28
#include <iostream>
using namespace std;

class V {
public:
  float vec[2];
  V(float a0, float a1) { vec[0]=a0; vec[1]=a1; } // constructor
       
  V operator+(V &arg) {                 // overloaded operator
     V res(0.0f,0.0f);
     for(int i = 0; i < 2; i++)
     res.vec[i] = vec[i] + arg.vec[i];  // <-- don't quite get this
                
     return res;
     }
};

int main(void) {
   V v1(0.0f, 1.0f);
   V v2(1.0f, 0.0f);
   V v3(0.0f, 0.0f);

   v3 = v1 + v2;

   cout << "(" << v3.vec[0] << ", " << v3.vec[1] << ")" << endl;

return 0;
}


So, I get the gist of the code, but I don't quite see how the line res.vec[i] = vec[i] + arg.vec[i]; does its stuff.

I can see that res.vec[i] is being assigned the result of vec[i] + arg.vec[i];, and the result is returned in res, but I don't get how the code figures out anything about vec[i] or arg.vec[i], since these are not passed, per se, to the overload function (i.e. as they would be in a normal function).

Nonetheless, I see there is a single parameter for the function, but nothing appears to be passed in the v3 = v1 + v2; line.

If someone could relieve me of my ignorance on this example, or in general with overloaded operators, then it would be much appreciated. Thanks!
Last edited on
V operator+(V &arg)

This operator is like a class function. It knows about all the member variables of the class, and it knows about all the parameters passed in.

What are those member variables? Here is a list of all the member variables of the class:
float vec[2];
What parameters are passed in?
V &arg


I don't get how the code figures out anything about vec[i] or arg.vec[i], since these are not passed, per se, to the overload function
vec doesn't have to be passed. It's a class member variable.
arg IS passed. Why do you think it isn't passed? Look, it's right here: V operator+(V &arg)


Last edited on
Well, the reason I say it doesn't appear to pass anything in, is because with a regular function you'd have something like x=func(x,y), but here there's nothing similar; it's just v3=v1+v2, and there's only one parameter on the function, but two variables are being added.
 
    res.vec[i] = this->vec[i] + arg.vec[i];  // <-- don't quite get this 

Added the this-> pointer for clarity.
this->vec[i] refers to the float member of the instance of V which invoked the operator function. More on this below.
arg.vec[i] refers to the the float member of the argument.
The + sign causes the two floats to be added together.
The result of the addition is stored in the float member of the result instance (res) which is returned by value at line 14.

23
24
 
  v3 = v1 + v2;

You can read this as:
 
  v3 = v1.operator + (v2);

v1's + operator function is invoked with v2 as the argument.
The result of the operator function is stored in v3.


v3=v1+v2 can be read as v3 = v1.operator+(v2).

It's not exactly correct, but it's a way to understand it.
Think of
my_object + other_object (1)
as being equivalent to
my_object.operator+(other_object); (2)
as being equivalent to
operator+(my_object, other_object); (3)

The difference is really just that a non-member function (3) has to be marked as a friend if we need to see private data members.
Last edited on
I get it! That makes sense now; thank you each so much for your replies and time; much appreciated!
Last edited on
Topic archived. No new replies allowed.