this pointer

Steven Schveighoffer schveiguy at yahoo.com
Wed Oct 21 14:00:07 PDT 2009


On Wed, 21 Oct 2009 16:53:20 -0400, Zarathustra  
<adam.chrapkowski at gmail.com> wrote:

> Why value of pointer to this is different in global function, member  
> function and constructor?
> //---------------------------------------------------------------------------------------------------
> module test;
> class Foo{
>   this(){
>     writefln("ctor 0x%08X", cast(dword)&this);
>   }
>
>   void func(){
>     writefln("func 0x%08X", cast(dword)&this);
>   }
> }
> import std.stdio;
>
> void main(){
>     Foo foo = new Foo;
>     writefln("main 0x%08X", cast(dword)&foo);
>     foo.func();
> }
> //---------------------------------------------------------------------------------------------------
> sample output:
> ctor 0x0012FEC8
> main 0x0012FEE8
> func 0x0012FEB8

&this is the address of the pointer itself (local function argument on the  
stack), not what it points to.  you want cast(void *)this.

-Steve


More information about the Digitalmars-d-learn mailing list