Overloading operators

Hey,
I took this example from the tutorial and modified it by overloading the operators: -, --, &.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "stdafx.h"
#include <iostream>
using namespace std;

class CVector {
  public:
    int x,y;
    CVector () {};
    CVector (int,int);
    CVector operator - (CVector);
    CVector operator --();
    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);
}
CVector CVector::operator --()
{
	CVector temp;
	temp.x = x--;
	temp.y = y--;
	return(temp);
}
CVector CVector::operator &(CVector param)
{
	return (param);
}
//--------- main -------------------
int main () {
  CVector a (3,1);
  CVector b (1,2);
  CVector c;
  CVector *p;
  p = new CVector;
  c = a - b;
  b--;
  p = &a;
  cout << "p = " << p->x << "," << p->y << endl;
  cout << "a = " << a.x << "," << a.y << endl;
  cout << "b = " << b.x << "," << b.y << endl;
  cout << "c = " << c.x << "," << c.y << endl;
  cin.get();
  return 0;
}

----------------------------
As you see, there is no arguments passed to the overloaded operator-- but there is for the overloaded operator&.
Is there any way to define the returned value in the operator& function if no arguments passed to it like the example with operator-- ?
Last edited on
How exactly would you pass an argument to the overloaded -- ?
Or not pass a second argument to the & operator?

The -- is a unary operator (taking only one operand).
The & is a binary operator (taking exactly two operands).

Hope this helps.
Last edited on
Topic archived. No new replies allowed.