Pointer to Object Questions

Jarrett Billingsley kb3ctd2 at yahoo.com
Sat Dec 30 09:07:32 PST 2006


"%u" <johnkirollos at yahoo.com> wrote in message 
news:en5lkb$v4r$1 at digitaldaemon.com...
> Hello Everybody
>
> I'm trying to understand how to get pointers working in D, so I wrote
> this example, and still several questions are without answer. Can
> somebody take a look on the code below, and comment on the 3
> questions in the output section? My main problem is with expressions
> in the form "MyClass* pObj".
>
> Thanks and Happy New Year
> John

Class variables are references to begin with.  You shouldn't have to use 
Class* types except under odd circumstances.

When you initialize pRecent, you are giving it the address of a variable on 
the stack (the "this" pointer is just a parameter to the class method).  So 
when you access it later, it gives garbage.  Same thing with getPtrToMe --  
since the 'this' pointer will probably end up at a different location on the 
stack, it won't give the same address as pRecent.  Accessing through 
getPtrToMe is working, but probably only through sheer luck of the 'this' 
variable on the stack not being clobbered. 




More information about the Digitalmars-d-learn mailing list