cast class references to void and back

BLS nanali at nospam-wanadoo.fr
Tue Sep 18 01:27:51 PDT 2007


 From the D spec;

Pointer Conversions
Casting pointers to non-pointers and vice versa is allowed in D, 
however, do not do this for any pointers that point to data allocated by 
the garbage collector.

Given :
Class CWin : CMsg
{
   Cwin pWnd = null;  // create a refrence

  /* C++
  pWnd = 
reinterpret_cast<CWin*>((long)((LPCREATESTRUCT)lParam)->lpCreateParams);
   */

   // D probabely
   pWnd = cast(CWin)(cast(LPCREATESTRUCT)lParam).lpCreateParams;

}

The LPCREATESTRUCT from WinUser.d
struct CREATESTRUCTA {
     LPVOID    lpCreateParams;   // Here I am
     HINSTANCE hInstance;
     HMENU     hMenu;
     HWND      hwndParent;
     // etc.
}
alias CREATESTRUCTA* LPCREATESTRUCTA;

So :
1) Do I have to put a cast(int) in there (where the (long) was in C++)
The reason you would use 'int' and not 'long' is that long in C++ is 
typically 32 bits and long in D is 64 bits.

2) I'm not 100% certain that it is guaranteed to work if you cast class 
references to void and back again.  Someone else might be able to 
confirm/deny this.
3) Following the D spec. this is also not guaranteed to work. confirm/deny ?

Thanks in advance
Bjoern



More information about the Digitalmars-d mailing list