cast class references to void and back

BLS nanali at nospam-wanadoo.fr
Tue Sep 18 03:53:59 PDT 2007


Regan Heath schrieb:
> BLS wrote:
>> BLS schrieb:
>>>
>>>  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
>>
>> Still someone out there who is still mental health ? 'cause const 
>> discussion :-)
>>
>> In this case just  another question,
>> In case that I define
>> Class CWnd
>> {
>>   CWnd* pWnd = 0;
>>
>> }
>> Do I have then in fact CWin** (from a C++ view) ?
> 
> Sort of, you have a pointer to a reference to a Cwnd class object. Where 
> a 'reference' is like but not exactly the same as a pointer.
> 
> Regan
Thanks for clarification Regan.
as you can see I,ve just copied the most of your comments ... ashamed 
... ,a bit ;-)
Bjoern


Bjoern



More information about the Digitalmars-d mailing list