|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
D - What does 'this' point to?
I have enough confusion about the pass by reference or is it value conventions
in D that I usually just pass my class or structs as inout parameters to mimic
Java or C like behaviour. However I am in the midst of debugging a rather
complex tree like data structure that uses a bit of recursion. I wanted to keep
track of which class reference is pointing to which class. I thought I would
simply print out the addresses of the class references and compare those with
one another to see. The surprise I found was that the pointers were changing
all the time. In fact the 'this' pointer changes when making a call from one
function to another within the same class! Observe the following code snippet:
// start code
import std.file ;
class Bigclass
{
int var = 0 ;
void ptr1() {
printf("In ptr1 var: %d this: %x\n", var, cast(uint) &this) ;
}
void ptr2() {
var = 2 ;
printf("In ptr2 var: %d this: %x\n", var,cast(uint) &this) ;
ptr1() ;
}
} // end Bigclass
int main(char[][] args)
{
Bigclass cl = new Bigclass() ;
cl.ptr2() ;
return(0) ;
}
// finish code
The output is:
In ptr2 var: 2 this: 12ff2c
In ptr1 var: 2 this: 12ff10
Thus the address of the this reference has actually changed from one method
call to another within the same class! This is going to make debugging pretty
tricky I think. Any comments on this behavior?
Apr 05 2005
Matthew wrote:In fact the 'this' pointer changes when making a call from one function to another within the same class! Observe the following code snippet: Apr 05 2005
In article <d2ufpj$2dhd$1 digitaldaemon.com>, Benjamin Herr says...Matthew wrote:In fact the 'this' pointer changes when making a call from one function to another within the same class! Observe the following code snippet: Apr 05 2005
"Matthew" <Matthew_member pathlink.com> wrote in message news:d2ucb2$2966$1 digitaldaemon.com...I have enough confusion about the pass by reference or is it value Apr 15 2005
|