Defining a custom *constructor* (not initializer!)
Jacob Carlborg
doob at me.com
Fri May 11 15:28:55 PDT 2012
On 2012-05-06 21:55, Mehrdad wrote:
> Is this possible in D2?
>
> i.e. I'm wondering if there is a way to do the object
> construction yourself, which would allow you to call the object's
> "constructor" (initializer) manually, and just return a pointer
> to the new object.
>
> Something along the lines of:
>
> class Window
> {
> static Window opConstruct(void* ptr)
> {
> // Assume ptr is already correctly sized and filled
> Window result = cast(Window)ptr;
> result.__ctor();
> return return result;
> }
> }
>
> It seems like overloading new() *almost* does this, but not
> quite: It only lets you allocate the memory, not call the
> constructor yourself.
>
> This would be useful, because some objects can have external
> constructors (notably when interfacing with C/C++, such as when
> using CreateWindow() in Windows), and you *cannot* call these
> constructors inside the object's "constructor" (initializer) due
> to re-entrancy issues.
Don't know if it helps but, have a look the DWT sources:
https://github.com/d-widget-toolkit/org.eclipse.swt.win32.win32.x86/tree/master/src/org/eclipse/swt
should probably be in the internal folder, widgets/Widget or widgets/Shell.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list