opAssign() still accepted for classes???

Steven Schveighoffer schveiguy at yahoo.com
Fri Apr 29 09:13:25 PDT 2011


On Fri, 29 Apr 2011 11:20:16 -0400, Alexander <aldem+dmars at nk7.net> wrote:

> On 29.04.2011 17:05, Steven Schveighoffer wrote:
>
>> opAssign is only invalid if you are assigning something that implicitly  
>> converts to that type.
>
>   Well, the documentation says: "The assignment operator = can be  
> overloaded if the lvalue is a struct aggregate, and opAssign is a member  
> function of that aggregate."

That's puzzling.  The D1 docs say "struct or class aggregate". So it was  
obviously removed in some version of the docs, but I'm not sure why.

Walter?

>> x = y is equivalent to x.opAssign(y).  So x is null, you are  
>> dereferencing a null pointer.
>
>   This I understand, of course - the main point is why it is accepted  
> for objects at all.

OK, some don't initially grasp that classes need to be initialized, I  
thought you were asking why the segfault occurs.

>   BTW, it seems that overloads can be static - what is the semantic of  
> static overload? (static opAssign() also fails).

Static opCall is possible, but I wasn't aware of the other operator  
overloads being possible.

Note that opAssign is a valid symbol name, so it can be used in places  
even where it doesn't overload assignment, such as a static or global  
function.  It just won't map to any operator usage.

-Steve


More information about the Digitalmars-d mailing list