December 31, 2006 Re: Pointer to Object Questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Kiro | "John Kiro" <johnkirollos@yahoo.com> wrote in message news:en6pvm$23fk$1@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 cast(uint)cast(void*)this 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 cast(uint)cast(CView*)this 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." |
January 01, 2007 Re: Pointer to Object Questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Thanks guys for all your comments.. I've found them invaluable in this tricky part of my D language exploration :-) As a summary: - CObj obj2=obj1; obj2 is a reference to obj1 (both obj1 & obj2 refer to the same object). So no new object is created. - CObj* pRefObj1=&obj1; pRefObj1 is the address of the reference of the object, not a pointer to the object itself.. TAKE CARE! - void* pObj1=cast(void*)obj1; pObj1 is a pointer to the object itself (as object pointers in C++) Happy New Year John |
January 01, 2007 Re: Pointer to Object Questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Kiro | "John Kiro" <johnkirollos@yahoo.com> wrote in message news:enavsd$49l$1@digitaldaemon.com... > As a summary: > > - CObj obj2=obj1; > > obj2 is a reference to obj1 (both obj1 & obj2 refer to the same > object). So no new object is created. > > - CObj* pRefObj1=&obj1; > > pRefObj1 is the address of the reference of the object, not a > pointer to the object itself.. TAKE CARE! > > - void* pObj1=cast(void*)obj1; > > pObj1 is a pointer to the object itself (as object pointers in > C++) Right on all counts. |
Copyright © 1999-2021 by the D Language Foundation