Pointer to Object Questions

%u johnkirollos at yahoo.com
Sat Dec 30 04:21:31 PST 2006


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


########################### code ###########################
import std.stdio;

class Point
{
	int m_x;
	int m_y;
	static Point recentPt;
	static Point* pRecent;

	this(int x, int y)
	{
		m_x=x;
		m_y=y;
		recentPt=this;
		pRecent=&this;			//I think incorrect
		//pRecent=cast(Point*)this;		//I think incorrect
too
		//pRecent=this;				//compile error
(cannot cast Point to Point*)
	}

	Point getMe()
	{
		return this;
	}

	Point* getPtrToMe()
	{
		return &this;
	}
}

int main()
{
	Point p1=new Point(5,5);
	writefln("p1: ( %d,%d )", p1.m_x, p1.m_y);
	writefln("recentPt: %d",cast(void*) Point.recentPt);
	writefln("Point referenced by Point.recentPt: ( %d,%d )",
Point.recentPt.m_x, Point.recentPt.m_y);
	writefln("Accessing p1 using getMe(): ( %d,%d
 )",p1.getMe().m_x, p1.getMe().m_y);
	writefln("pRecent contains: %d",cast(void*) Point.pRecent);
	writefln("Point referenced by Point.pRecent: ( %d,%d )",
Point.pRecent.m_x, Point.pRecent.m_y);
	writefln("p1.getPtrToMe() returns: %d",cast(void*)
p1.getPtrToMe());
	writefln("Accessing p1 using getPtrToMe(): ( %d,%d
 )",p1.getPtrToMe().m_x, p1.getPtrToMe().m_y);

	Point p2=new Point(8,8);
	writefln("\np2: ( %d,%d )", p2.m_x, p2.m_y);
	writefln("recentPt: %d",cast(void*) Point.recentPt);
	writefln("Point referenced by Point.recentPt: ( %d,%d )",
Point.recentPt.m_x, Point.recentPt.m_y);
	writefln("Accessing p2 using getMe(): ( %d,%d
 )",p2.getMe().m_x, p2.getMe().m_y);
	writefln("pRecent contains: %d",cast(void*) Point.pRecent);
	writefln("Point referenced by Point.pRecent: ( %d,%d )",
Point.pRecent.m_x, Point.pRecent.m_y);
	writefln("p2.getPtrToMe() returns: %d",cast(void*)
p2.getPtrToMe());
	writefln("Accessing p2 using getPtrToMe(): ( %d,%d
 )",p2.getPtrToMe().m_x, p2.getPtrToMe().m_y);

	return 0;
}

########################### Output ###########################

p1: ( 5,5 )
recentPt: 8A0FE0							--> I guess it
should be the address of p1
Point referenced by Point.recentPt: ( 5,5 )
Accessing p1 using getMe(): ( 5,5 )
pRecent contains: 12FF04
Point referenced by Point.pRecent: ( 3,4277824 )	--> what's
wrong here?
p1.getPtrToMe() returns: 12FE9C				--> why it's
not equal to pRecent (they both should be "&this"?
Accessing p1 using getPtrToMe(): ( 5,5 )	--> how come it works,
while using "Point.pRecent" does not?

p2: ( 8,8 )
recentPt: 8A0FD0							--> OK, this
shows that each point is 16-byte long ( &p1 + 16 )
Point referenced by Point.recentPt: ( 8,8 )
Accessing p2 using getMe(): ( 8,8 )
pRecent contains: 12FE6C
Point referenced by Point.pRecent: ( 3,4277824 )
p2.getPtrToMe() returns: 12FE04
Accessing p2 using getPtrToMe(): ( 8,8 )



More information about the Digitalmars-d-learn mailing list