www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - *this

reply Paul D. Anderson <paul.d.removethis.anderson comcast.andthis.net> writes:
Looking at Don Clugston's BigInt code I see usage of "*this":

BigInt opMulAssign(T: BigInt)(T y) {
       *this = mulInternal(*this, y);
         return *this;
     }

I think I know what it does (passes this by reference) but I can't find any
documentation that explains the usage. Can anyone point me to a source??

Paul
Apr 24 2009
parent reply Don <nospam nospam.com> writes:
Paul D. Anderson wrote:
 Looking at Don Clugston's BigInt code I see usage of "*this":
 
 BigInt opMulAssign(T: BigInt)(T y) {
        *this = mulInternal(*this, y);
          return *this;
      }
 
 I think I know what it does (passes this by reference) but I can't find any
documentation that explains the usage. Can anyone point me to a source??
 
 Paul
 
 
It's passing by value, since BigInt is a struct, not a class. 'this' is a pointer to the struct, so *this is the struct itself. In the case of BigInt, the struct only contains an bool and a dynamic array, for a total of 10 bytes or so, so it's not much bigger than a pointer. I believe the behaviour of 'this' in structs changed recently (but probably only in D2?) The BigInt wrapper class would be a bit different in D2. BTW BigInt uses structs, not classes, and part of the reason for this is that you _cannot_ have value semantics with a class.
Apr 24 2009
parent Paul D. Anderson <paul.d.removethis.anderson comcast.andthis.net> writes:
Don Wrote:

 Paul D. Anderson wrote:
 Looking at Don Clugston's BigInt code I see usage of "*this":
 
 BigInt opMulAssign(T: BigInt)(T y) {
        *this = mulInternal(*this, y);
          return *this;
      }
 
 I think I know what it does (passes this by reference) but I can't find any
documentation that explains the usage. Can anyone point me to a source??
 
 Paul
 
 
It's passing by value, since BigInt is a struct, not a class. 'this' is a pointer to the struct, so *this is the struct itself. In the case of BigInt, the struct only contains an bool and a dynamic array, for a total of 10 bytes or so, so it's not much bigger than a pointer. I believe the behaviour of 'this' in structs changed recently (but probably only in D2?) The BigInt wrapper class would be a bit different in D2. BTW BigInt uses structs, not classes, and part of the reason for this is that you _cannot_ have value semantics with a class.
Thanks, Don. Paul
Apr 24 2009