Create an object whose state changes on assignment I find it deeply weird that this is possible in Ruby (I won't immediately say how):

obj = #code redacted

print obj.state # Some value.

LValue = obj

print obj.state # Different value!


Your challenge is to create code roughly of this form. Create an object and assign it to a variable. It should have some defined attribute (or deterministic, idempotent method) like state above, that changes after the object is assigned to a new identifier (LValue above), even if you still use the old identifier (obj above) to refer to it.

Edit for emphasis: state or the equivalent must be idempotent, so creating an accessor that modifies the value, or for any other reason returns different results when called several times in a row, isn't a valid solution. Or, more simply, it has to be the assignment that changes the state.

Any language with assignment is eligible, although there are probably some where there's no fully legitimate solution. I'll post my Ruby answer if nobody else gets it after a few days, and accept highest-voted answers on a rolling basis.
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