How to return the current object

Ali Çehreli acehreli at yahoo.com
Sun Sep 25 19:28:28 PDT 2011


On Sun, 25 Sep 2011 20:20:59 -0600, alex wrote:

> Is there a way to return the current object in any part of the code,

I presume you mean returning self from a member function:

class C
{
    C foo()
    {
        return this;
    }

    C bar()
    {
        return this;
    }
}

void main()
{
    auto o = new C;
    o.foo().bar();
}

> even from void main? (which is still a type void). I have tried the this
> thing but it doesn't work

No, you can't return any object from main if the return type is void. You 
may define main as returning int, to communicate the termination status 
of the program to the environment that started it:

int main()
{
    return 0;
}

Ali


More information about the Digitalmars-d-learn mailing list