overloading operators

Hello, Could anyone please explain me sequence by sequence about what is happening in this code?
This is how much i could figure out on my own:
1) CVector type object foo is declared
2) Foo gets variables x,y, 1. constructor is skipped, because it's empty, 2. constructor receives values 3,1 and transmits them to x,y and from now on i am very confused
What does this "CVector operator + (const CVector&);" line do?
My guess would be that function contructor Cvector overloads operator + for (const CVector&) and const CVector& itself is a declaration of the objects' variables in action, so for foo it should be const x(3), y(1)
I don't know about the rest and sorry i'm asking a very simple question, i've been on this code for hours, trying to search for answers and even my programming teacher is on vacation
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;
}
I'm not entirely sure what base you're starting from. Do you think the overloaded operator is a constructor itself?

It's a function called from foo on line 24. It creates a CVector object named temp (which itself calls the default constructor). It then sets temp's x and y variable to the respective sums of x and y variables in both the foo and bar objects. It then returns a CVector object which is stored in result.
Thank you, i understand the whole code now.
Topic archived. No new replies allowed.