still confused about call by reference

Jarrett Billingsley kb3ctd2 at yahoo.com
Tue Oct 30 19:01:43 PDT 2007


"Bill Baxter" <dnewsgroup at billbaxter.com> wrote in message 
news:fg8mpg$15gl$1 at digitalmars.com...
> Saaa wrote:
>> What does this mean exactly ?
>>
>> initialize an instance of a struct on the heap
>> without factoring out the initialization to _another_ function
>
> You can say
>   MyStruct *x = new MyStruct;
>
> But even with a static opCall defined, this doesn't work
>   MyStruct *x = new MyStruct(a,b,c);
>
> You have to do something like:
>   MyStruct *x = new MyStruct;
>   *x = MyStruct(a,b,c);
>
> Or that's what I guess he means, at least.  I haven't actually tried the 
> code above to see what it will do.
>
> --bb

I was thinking more along the lines of

struct S
{
    int x, y;

    void init(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    static S opCall(int x, int y)
    {
        S s;
        s.init(x, y);
        return s;
    }
}

..

// Stack
S s = S(3, 4);

// Heap
S* t = new S;
t.init(5, 6); 




More information about the Digitalmars-d-learn mailing list