In short:
How do I define operator= for my class such that it compiles only if rhs is 0 (obj = 0)
but gives compilation error if rhs is non-0 value.
I know it is possible, forgot how.
Longer:
I have class C. I want to allow assignment obj = 0 for object of this class (which means, reset the object), but assignment from no other integer or pointer is defined. No conversion from integer or from pointer is defined other than obj=0.
C obj;
obj = 0; // reset object
Inside the operator=, I can do assert(rhs == 0), but that’s not good enough.
I know it is possible
to define operator= such that
it gives compilation error if rhs is not 0. Forgot details.
Can anybody refill ?
Thanks
Use a pointer-to-member:
Since you can’t access
foo::rhs_must_be_zero, you cannot name a pointer to member from this class. The only pointer to member you can name is the null pointer, aka the literal zero.Demo at http://ideone.com/bT02z