www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - this pointer

reply Zarathustra <adam.chrapkowski gmail.com> writes:
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
Oct 21 2009
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Oct 21 2009
parent reply Zarathustra <adam.chrapkowski gmail.com> writes:
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 ;)
Oct 21 2009
parent BCS <none anon.com> writes:
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.
Oct 21 2009