opCall example

Saaa empty at needmail.com
Thu Jan 31 03:56:24 PST 2008


>>> S s = 3;    // sets s.a to 3
>>> S t = s;    // sets t.a to 3, S.opCall(s) is not called
>>
>> opCall is used like this:
>>
>> S s = S(3);
>> S t = S(s);
>>
>> I don't know why your first statement works. I think it's because S is 
>> just a wrapper for an int, so an int can be implicitly casted to S.
>
> the first statement works cause he created an opCall for S to be called 
> with just an int. the second statement doesn't call S opCall(S) cause a 
> struct assign expression just performs a memory copy when S t = s; to have 
> the second opCall called u would have to write what Ary suggested: S t = 
> S(s);

Should I see this as the memory copy having a preference above the opCall(S)
or, calling opCall(int) like S s=3 as being a special case?

Because syntactically there isn't any difference:
3 is of type int thus invoking opCall(int)
s is of type S thus invoking ... opCall(S) :) 




More information about the Digitalmars-d-learn mailing list