Operator overloading -- lets collect some use cases
KennyTM~
kennytm at gmail.com
Wed Dec 31 02:01:14 PST 2008
Christopher Wright wrote:
> Don wrote:
>> Some observations based on the use cases to date:
>> (1)
>> a += b is ALWAYS a = a + b (and likewise for all other operations).
>> opXXXAssign therefore seems to be a (limited) performance
>> optimisation. The compiler should be allowed to synthesize += from +.
>> This would almost halve the minimum number of repetitive functions
>> required.
>
> Not quite true:
> class A
> {
> int value;
> A opAdd(A other) { return new A(value + other.value); }
> A opAddAssign(A other) { value += other.value; }
> }
>
> class B
> {
> A a;
> this (A value) { a = value; }
> }
>
> void main ()
> {
> auto a = new A;
> auto b1 = new B(a);
> auto b2 = new B(a);
> auto a2 = new A;
> b1.a += a2; // okay, b1.a is b2.a
> b1.a = b1.a + a2; // now b1.a !is b2.a
> }
Both expression should yield the same _value_, not necessarily the same
_reference_, so “b1.a == b2.a” should return true.
More information about the Digitalmars-d
mailing list