Pointer to Object Questions

John Kiro johnkirollos at yahoo.com
Sat Dec 30 12:34:28 PST 2006


== Quote from Jarrett Billingsley (kb3ctd2 at yahoo.com)'s article
> "%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.

So, as BCS said, "&this" is the address of the reference to the
object (I have a comment on this, and I'll post it in a separate
reply), and the reference is passed to the member function on the
stack. So in conclusion, I was using the address of a temp stack
variable.

Thanks for your useful comment
John


More information about the Digitalmars-d-learn mailing list