On Tue, 16 Apr 2013 15:57:09 +0100, Tofu Ninja <emmo...@purdue.edu> 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 question always question "fully intended" on a case by case basis:
http://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197

I agree that grouping functions together that should be used together, or on the same type of object is a good idea from an organisational point of view but that shouldn't be the only reason for making them class member functions.

R

--
Using Opera's revolutionary email client: http://www.opera.com/mail/

Reply via email to