Problem overloading operator for a struct with an immutable member

Nicolas Sicard via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 26 02:02:53 PDT 2015


On Thursday, 26 March 2015 at 04:57:55 UTC, ketmar wrote:
>
> by the way. do you know that you still CAN overload 
> postincrement
> operation? yes, the code is still here, and it works... 
> somethimes. ;-)

Thnaks. Indeed, this works:
---
struct S
{
     int i;
     immutable(Object) o;

     void opAddAssign(int j) { i += j; }
     S opPostInc() { ++i; return this; }
     void opAssign(S other) {}
}

unittest
{
     S s;
     ++s;
     assert(s.i == 1);
     s++;
     assert(s.i == 2);
}
---

Old operator overloading to the rescue !


More information about the Digitalmars-d-learn mailing list