In order to make the object as amorphous to int as possible, from the user perspective, you need to provide the following definitions:
1 2 3 4 5
|
struct S {
S(int) { /*...*/ }
int operator= (int right) { /*...*/ return (int)(*this); } //optional, for improving performance
operator int() { return 0/*substitute...*/; }
};
|
I
made wanted to make the assignment return reference, because:
Standard wrote: |
---|
The result of the assignment operation is the value stored in the left operand after the assignment has taken place; the result is an lvalue. |
And I think that they mean assignable lvalue. But it didn't work. See the edits.
On a side note, I think you should use properties. They are usually handled in C++ with getters and setters, and this is not the most graceful solution, but it is the best available. Your register is not an int, and making it an int, so that it is convenient in specific context is not elegant. Next, you'll be making it bool for convenience in some other context, and your object will become the chimera of software design. Sometimes it is useful, but it is apparently cheating.
There is alternative way to implement properties. You could also use sub-object for the property that keeps pointer to the host object ("parent pointer") and in this way establishes two-way communication. This allows the host object to update its state when the sub-object is updated. The sub-object can be amorphous to int and it can provide the user with alternative personality for your main object. But this parent pointer business is redundant, and with proper support for properties in C++, it could have been avoided entirely.
Regards
EDIT: Messed up the assignment. Had to return the left operand, not the right one.
EDIT: The assignment operator is tough in this case. If you return the right side as lvalue, you will not follow the required protocol, and the operand on the right has to be lvalue. If you return the left side, it is not int. So finally, I decided to return the left side converted to int, and not as reference. This is slight deviation from the conventional case.