Thread overview
this pointer
Oct 21, 2009
Zarathustra
Oct 21, 2009
Zarathustra
Oct 21, 2009
BCS
October 21, 2009
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
October 21, 2009
On Wed, 21 Oct 2009 16:53:20 -0400, Zarathustra <adam.chrapkowski@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
October 21, 2009
Steven Schveighoffer Wrote:

> On Wed, 21 Oct 2009 16:53:20 -0400, Zarathustra <adam.chrapkowski@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

Thank's a lot. God blesses C++ for the '->' operator ;)

October 21, 2009
Hello Zarathustra,

> Thank's a lot. God blesses C++ for the '->' operator ;)

God curse C++ for the '->' operator. 

And I'm not making a Joke, I hate that thing! 

It makes a distinction between value and reference semantics where they don't matter (lookup) and does nothing for the cases (assignment, arg passing) where it does.