Thread overview
Struct toString() & this pointer question
Apr 09, 2007
Tom
Apr 09, 2007
BCS
Apr 09, 2007
Tom
April 09, 2007
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;
April 09, 2007
Structs are pass by value. The *s pushes the struct onto the stack and then the toString function is called on that struct.


April 09, 2007
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.
> 
>