Defining a custom *constructor* (not initializer!)

Steven Schveighoffer schveiguy at yahoo.com
Mon May 7 12:39:05 PDT 2012


On Mon, 07 May 2012 15:08:16 -0400, Mehrdad <wfunction at hotmail.com> wrote:

> On Monday, 7 May 2012 at 17:04:08 UTC, Steven Schveighoffer wrote:
>> Not really, but then again, if you are not placing the class into the  
>> GC heap, who cares?  You have to manually delete anyways, just use your  
>> specialized 'delete' function instead of delete.
>>
>> -Steve
>
> No, I *am* placing it on the heap.

You hadn't made that clear.

In your first post, I was assuming your ptr came from some non-GC  
allocated space, which is why you wanted the ability to intercept it.

> I'm just asking if I can call the constructor manually, because
> (like I wrote in my first post...) sometimes the C code you're
> interoperating with takes control away from you, and just calls a
> callback on your behalf when constructing the object.

I wasn't sure, but I just tried it out:

import std.stdio;
extern(C) void *_d_newclass(TypeInfo t); // this is defined in  
rt/lifetime.d  I cheated and used this, because I didn't want to have to  
type everything that was in it :)

class C
{
     int x;
     this(int x){this.x = x;}
}

void main()
{
     C c = cast(C)_d_newclass(typeid(C));
     c.__ctor(1);
     writeln(c.x); // outputs 1
}

Seems to work

-Steve


More information about the Digitalmars-d mailing list