Daniel said:
Eric Gunnerson [MS] wrote:
There is no way to overload the assign operator.
That is unfortunately what I thought.
You may be able to get what you want by overloading a conversion
operator to convert from y to x; that would allow you to write "x
= y";
No I want to overload the default behavior when one says:
X x;
X y;
// Do something with x to change it.
y = x;
where X is one of my own __value classes.
I think it is a weakness of the CLR that one can't override this
behavior to supply one's own functionality instead of the default
behavior for user-defined __value types. I admit I am not sure what
the default behavior is, whether it is a byte-by-byte copy of data
from x to y, or whether it is a field-by-field copy of data from x
to y, or whether it takes into account property values and gets and
sets the appropriate properties when moving the data. I know that
when X is a __value class with properties, I want the get_ and set_
method for each property to be called if I say y = x, but I suspect
that is not the case. That is the reason I was hoping to override
the default assignment somehow.
My workaround is to provide a 'void X::Assign(X)' in order to do
the assignment, so that instead of writing y = x, I would write
instead y.Assign(x), but this is much less natural than the normal
assignment operator and y = x.
Needless to say, in C++ one can overload the default assignment
operator with 'X & operator = ( X /* or const X & */);'. Maybe CLR
should revisit this inability and make changes in the future which
allows overriding the assignment operator for user-devined __value
classes possible.
Interesting...this is one of those features(operator overloading in
general, outside of mathematics of course) I think should be left to
die a slow death.
It is alive and well, fortunately, but I am not going to get into a
polemic about it. I know only too well the arguments on both sides (
C++ has had it from its inception ) and I know how much I approve of
it myself and that is all I will say.
Clearly .NET supports it although it is not CLS compliant. I just
wanted to use it in my own code for clarity, while providing a CLS
Assign member function also for other .NET languages.
Rather than get into an argument about overriding the built-in =
operator for value types, I will present a situation where it may
not do what I want it to do. I have a value type, let us call it X
as in the above example, which has a property, and also an event
which is triggered when the property changes. So in my property set
function, I trigger the event. If I assign one X to another using
the built-in = operator, does my property set function get called or
does the built-in = operator just copy the underlying X data from
one to the other ? If the latter, then my event never gets
triggered, and event handlers which need to know when the property
changes never get called when using = to assign one object of the
value type to another.
In the Assign member function of value type X, I assign the property
from one to another which assures that the event gets triggered. If
I could override the built-in assignment operator = , I would do the
same. Obviously I can use Assign instead, but I have to remember to
use it rather than the more natural = assignment.