still confused about call by reference
Saaa
empty at needmail.com
Tue Oct 30 19:51:24 PDT 2007
>> 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);
>
Ah, I see what you mean.
making your own _new would fix this, right? But yeah I think a constructor
would be much better ;)
I always put everything on the stack for speed purposes. I now that I think
about it, I also never throw any struct array away.
I try to allocate all necessary memory at the beginning of my programs.
Ticks are allot more scarce than memory in my programs :D
Anyway thanks.
More information about the Digitalmars-d-learn
mailing list