www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - this - getting a reference to `this'

reply Jay <Jay_member pathlink.com> writes:
Consider the following: 
 
----8<------ 
 
import std.stdio; 
 
class Client 
{
public: 
	this(char[] n) 
	{ 
		name = n; 
		server.register(&this); 
	}
char[] name; 
} 
 
class Server 
{ 
  public: 
	void register(Client* c) 
	{
printf("Registering %*s (%p)\n", c.name, c); 
	} 
} 
 
Server server; 
 
int
main() 
{ 
	server = new Server(); 
	Client c = new Client("c");
printf("Client c is at %p\n", &c); 
	return 0; 
} 
 
------>8---- 
 
I get the
following output using the Digital Mars D Compiler v0.105 for x86 
Linux:
Registering c (0xbffff410) 
Client c is at 0xbffff42c 
 
I was expecting both
pointer values to be the same, like in C++. What am I 
missing? 
 
Thanks, 
Jay 
Nov 01 2004
parent reply Ant <duitoolkit yahoo.ca> writes:
On Tue, 02 Nov 2004 03:25:38 +0000, Jay wrote:

this and c are references
remove all & and *


 		server.register(&this); 
 	void register(Client* c) 
 printf("Client c is at %p\n", &c); 
Nov 01 2004
parent Jay <Jay_member pathlink.com> writes:
In article <pan.2004.11.02.04.17.01.434756 yahoo.ca>, Ant says... 
 
On Tue,
02 Nov 2004 03:25:38 +0000, Jay wrote:
 
this and c are references 
remove
all & and *
 
 
 		server.register(&this);  
 	void register(Client* c)
 printf("Client c is at %p\n", &c);  
Thanks, that did it.
Nov 01 2004