Pointer to Object Questions

Jarrett Billingsley kb3ctd2 at yahoo.com
Sat Dec 30 17:15:06 PST 2006

"John Kiro" <johnkirollos at yahoo.com> wrote in message 
news:en6pvm$23fk$1 at digitaldaemon.com...
> Speaking about casting, there is usually a need to cast a reference to an
> integer & vice-versa; I'm specifically pointing to the case of using a 
> function
> like the Win32 API function SetWindowLong(), in order to link a window 
> with a
> corresponding object. Here is how I did it:
>  //Linking view window to view obj:
>  SetWindowLong(hwndView,0,cast(uint)(cast(CView*)this));
>  //retrieve the object given the window handle:
>  cast(CView)cast(CView*)GetWindowLong(hwndView,0);
> Casting to void* instead of CView* also compiled without errors.
> So is this the usual way of doing such thing? I mean double casting?
> Because casting an object to int or vice-versa is rejected by the 
> compiler.

Yes.  Though you should use the


form, as this is the more generally accepted way.  It makes it obvious that 
you're getting the pointer value of the reference, compared to


Since 'this' is of type CView, casting CView to CView* doesn't really make 
sense, but casting to void* does -- it says "I want a raw pointer." 

More information about the Digitalmars-d-learn mailing list