www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Struct toString() & this pointer question

reply Tom <tom nospam.com> writes:
Hey guys,

   having the following code:

# struct S
# {
#   int m= 1234;
#	
#   static S* build_one() {
#     auto new_inst= new S;
#     return new_inst;
#   }
#
#   char[] myToString() {
#     return format("(%s)={this=%x,m=%s}", typeof(this).stringof, this, m);
#   }
#	
#   char[] toString() {
#     return format("(%s)={this=%x,m=%s}", typeof(this).stringof, this, m);
#   }
# }

# void main()
# {
#   auto s= S.build_one();
#   writefln("Builded a new instance at %x (%s)", s, s.myToString);
#   writefln("Builded a new instance at %x (%s)", s, *s);
#   writefln("Builded a new instance at %x (%s)", s, s.toString);
# }

The output was:

Builded a new instance at 910FE0 ((S *)={this=910FE0,m=1234})
Builded a new instance at 910FE0 ((S *)={this=13FEEC,m=1234})
Builded a new instance at 910FE0 ((S *)={this=910FE0,m=1234})

I wonder why is that in the second line, toString is printing an 
alternative this pointer?

Regards,
--
Tom;
Apr 08 2007
parent reply BCS <ao pathlink.com> writes:
Structs are pass by value. The *s pushes the struct onto the stack and then 
the toString function is called on that struct.
Apr 08 2007
parent Tom <tom nospam.com> writes:
Stupid me... of course, thanks!

BCS escribió:
 Structs are pass by value. The *s pushes the struct onto the stack and 
 then the toString function is called on that struct.
 
 

Apr 08 2007