What should opAssign return, finally answered!

downs default_357-line at yahoo.de
Tue Oct 23 02:32:31 PDT 2007

Charles D Hixson wrote:
> What I would expect is:
> class A
> {
>   A opAssign(A a)
>   {
>      // do stuff here
>      return this;
>   }
> }
> but the only examples that I can find (variant.d) return *this ... which
> leaves me quite confused.  Is this something special that Variant is
> doing to allow it to handle multiple types (I didn't follow the
> internals), or do I have what is supposed to be returned wrong?
Variant is a struct.
The this of a struct is a pointer.
Ergo it has to dereference it, since opAssign returns the assignee for
purposes of assignment chaining (a=b=c).

Hope that clears things up.

More information about the Digitalmars-d-learn mailing list