Create an object whose state changes on assignment
C++

This is trivial using the right tools.


#include &lt;iostream&gt;

using namespace std;

class Obj {
public:
   int state;

   Obj&amp; operator= (Obj&amp; foo) {
      foo.state++;
      this-&gt;state = foo.state - 2;
      return *this;
   }
};

int main() {
   Obj a, b, c, d;
   a.state = 3;
   b.state = 4;

   cout &lt;&lt; a.state &lt;&lt; " " &lt;&lt; b.state &lt;&lt; "\n";

   c = a;
   d = b;

   cout &lt;&lt; a.state &lt;&lt; " " &lt;&lt; b.state &lt;&lt; " " &lt;&lt; c.state &lt;&lt; " " &lt;&lt; d.state &lt;&lt; "\n";

   return 0;
}


Output:

3 4
4 5 2 3