overloading ++

Hello, I'm trying to overload ++ operator:
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

class MyClass
{
public:
int x;

MyClass(){x=0;}

MyClass operator ++(int);
};

MyClass MyClass::operator ++(int a)
{
  MyClass tmp=*this;
  this->x++;
  return tmp;
}


int main()
{

MyClass a;
MyClass b;
 
cout <<"a.x: "<<a.x<<endl; // 0
b=a++;
cout <<"b.x: "<<b.x<<endl; // 0
cout <<"a.x: "<<a.x<<endl; // 1

}

My questions:
1. When shgould I return const object, eg:
const MyClass &operator ++(int);
instead of
MyClass &operator ++(int); ?
2. Why ++ must take int as parameter?

Thanks for help.
1. That's not an object. That's a reference. You should return a constant reference when you don't want to allow changing the object, which is very often.
2. To distinguish it from the prefix version.
Prefix increment should return *this by reference;
Postfix increment must return a new instance by value.
Topic archived. No new replies allowed.