On Thu, 01 Sep 2011 15:08:29 -0400, Alex Rønne Petersen <[email protected]> wrote:

Hi,

Consider this:

struct S
{
    static S opAssign(int rhs)
    {
        return S();
    }
}

void main()
{
    S s;
    s = 1;
}

And:

struct S
{
    S opAssign(int rhs)
    {
        return S();
    }
}

void main()
{
    S s;
    s = 1;
}

Both will compile. I don't get it. I can understand it in the case of the static opAssign, but with the non-static one, what does 'this' refer to inside opAssign? And why are both seemingly allowed?

s = 1 is rewritten as s.opAssign(1). So in the non-static version, 'this' refers to the struct you called the function with (in your example, s).

so:

struct S
{
   int x;
   S opAssign(int rhs)
   {
      x = rhs;
      return this;
   }
}

void main()
{
   S s;
   s = 5;
   assert(s.x == 5);
}

Since you are allowed to call static member functions, s.opAssign(1) for the static one succeeds, but some of us are pushing to make this invalid. It makes things very confusing when you call static functions, but it looks like you are calling non-static member functions.

-Steve

Reply via email to