# How post-increment operator works with user defined types?

• A+
Category：Languages

I have a difficulty on how this post-increment operator works for user-defined types: As I guess the Post-Increment operator `operator++(T _unused)` saves the original value first (into the expression) then increment the variable. e.g:

``int x{5}; int y{x++}; ``

So `x = 6` and `y = 5` and that's ok.

But here is an example of user-defined:

``struct s{     int x;     s(int m_) : x(m_){}     s operator++(int){ x++; return *this;} };   int main(){      s a(0);     a.x = 7;     cout << "a::x: " << a.x << endl;      s b(a++);      cout << "a::x: " << a.x << endl; // 8     cout << "b::x: " << b.x << endl; // 8 ?!      int t = 9;     s c(t++);      cout << "c::x: " << c.x << endl; // 9     cout << "t: " << t << endl; // 10 ok      cout << endl;     return 0; } ``

As you can see above: `s b(a++)` will make the values of `a.x` and `b.x` `8`? I can't understand why?

Thank you guys for your help and time and efforts.

How post-increment operator works with user defined types?

The way you write it, as it is mostly a regular method.

It is good practice to avoid surprise, and so mimic behaviour of built-in types is good.

To mimic built-in type as `int`, you might fix implementation to:

``struct s {     int x;     explicit s(int m_) : x(m_){}     s& operator++(){ ++x; return *this;} // pre-increment     s operator++(int){ auto res = *this; ++*this; return res;} // post-increment }; ``