Thread overview |
---|
November 02, 2004 this - getting a reference to `this' | ||||
---|---|---|---|---|
| ||||
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 |
November 02, 2004 Re: this - getting a reference to `this' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jay | 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);
|
November 02, 2004 Re: this - getting a reference to `this' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | 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. |
Copyright © 1999-2021 by the D Language Foundation