Ref and class function calls?
John Colvin
john.loughran.colvin at gmail.com
Tue Apr 16 08:12:38 PDT 2013
On Tuesday, 16 April 2013 at 14:57:11 UTC, Tofu Ninja wrote:
> On Tuesday, 16 April 2013 at 14:33:21 UTC, John Colvin wrote:
>> A member function cannot modify it's own 'this' pointer.
>>
>> However, a free function can do it happily, which when
>> combined with UFCS gives you the same syntax and behaviour:
>>
>> class A {
>> //......
>> }
>>
>> void replace(ref A a)
>> {
>> a = new A();
>> }
>>
>> void main() {
>> A a = new A();
>> A b = a;
>> assert(b is a);
>> b.replace();
>> assert(!(b is a));
>> }
>>
>> http://dpaste.dzfl.pl/147f69e1
>
> Yes... this is what I feared. I knew I could do it like that
> but I was hoping a more elegant solution was available, seems
> like bad design to have a function that is fully intended to be
> a class function but not actually be able to declare it within
> the class block.
I would argue that it is better as a separate function.
Having a member function that reassigns 'this' seems a long way
from least-surprise (although with UFCS the user might be none
the wiser anyway).
More information about the Digitalmars-d-learn
mailing list