Overloading the assignment operator, right now!

Oskar Linde oskar.lindeREM at OVEgmail.com
Fri Sep 15 00:57:53 PDT 2006


Georg Wrede wrote:
> Hasan Aljudy wrote:
>> Georg Wrede wrote:
>>
>>> Overloading of the assignment operator is not allowed in D.
>>>
>>> But no problem, we can do it right now, in spite of it.
>>>
>>> Just overload the opCatAssign operator instead, and use ~= where = 
>>> would be used.
>>>
>>> This way we might gather enough use cases to really know whether it 
>>> is good or bad in practice.
>>
>> <snip>
>>
>> Why do you have to think in C++ when coding in D?
> 
> I don't. Do you?
> 
> Seriously, if you look back in the archives, I've been pretty voluminous 
> in my support of several ideas and opinions. Getting assignment operator 
> overload hasn't been one of them.
> 
> Then again, some other people have really wanted it, and I stumbled on a 
> method to try it out with, which I wrote here. Now they can try it out, 
> without Walter having to make changes to the language.

There is more to it than having an operator to overload. You want to be 
able to overload both assignment and copying.

Given

struct T { ... opCatAssign(T t) {...} }
void func(T t) {...}
T a,b;

You take care of

	a ~= b;

but not

	func(b);

where a copy is created too.

/Oskar



More information about the Digitalmars-d mailing list