Initializing a class pointer

Simen kjaeraas simen.kjaras at gmail.com
Sat Feb 26 15:52:22 PST 2011


Tyro[a.c.edwards] <nospam at home.com> wrote:

> I'm trying to convert some c++ code that defines
>
> T func(par...)
> {	
>    Controller * pCtrl = WinGetLong<Controller *> (hwnd);
>    .
>    .
>    .
>    switch(msg)
>    {
>    case FirstMatch:
>       pCtrl = new Controller (hwnd, reinterpret_cast<CREATESTRUCT *>  
> (lParam));
>       break;
>    }
> }
>
> I'm not sure why I need a pointer to the class, just trying to figure it  
> out.

Ah. You would not need a pointer to the class in D. Instead, your function
would look something like this:

T funct(par...)
{
     auto pCtrl = WinGetLong!Controller(hwnd);
     ...
     switch(msg)
     {
     case FirstMatch:
         pCtrl = new Controller(hWnd, cast(CREATESTRUCT*)lParam);
         break;
     }
}

C++ classes are in some ways more akin to D structs, in that:

class A {};

void foo(){
     A bar;
}

bar would be allocated on the stack in C++, while in D bar would be a
pointer to a class instance on the heap. (well, it would be null, but
when you set it to something, that something would reside on the heap)

-- 
Simen


More information about the Digitalmars-d-learn mailing list