Pointer to Object Questions

John Kiro johnkirollos at yahoo.com
Sat Dec 30 12:46:50 PST 2006


== Quote from BCS (nothing at pathlink.com)'s article
> %u wrote:
> > 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
> >
> First thing first (and this is a common gotcha) All objects are
accessed
> by reference.
> class Foo{}
> Foo* f;	// this is a pointer to a reference (pointer) to an object
> Foo g; // this is a reference to an object
> regular types are not
> int* p; //pointer to int


OK, but why does the compiler accepts something like:

  Point* pp1 = &p1;
  writefln("Accessing p1 using pp1: ( %d,%d )", pp1.m_x, pp1.m_y);

I mean it seems that the leftside of "." can be either an object
reference (p1) or a pointer to an object reference (pp1). Is this
indeed the case?

BTW, I tested the above 2 statements in main(), and the result was
correct.

Anyway, I guess I have to give up using pointers to objects. (may be
it's OK in structs, not classes??)

Regards
John



More information about the Digitalmars-d-learn mailing list