www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opAssign for structs

reply Sean Silva <chisophugis gmail.com> writes:
In the language definition <http://d-programming-language.org/struct.html>, it
says:

 Struct assignment t=s is defined to be semantically equivalent to:
   t = S.opAssign(s);
 where opAssign is a member function of S:
   S* opAssign(S s)
   {   ... bitcopy *this into tmp ...
       ... bitcopy s into *this ...
       ... call destructor on tmp ...
       return this;
   }
I'm struggling with this on 4 fronts: 1. What is `this`, when opAssign is called off of the type? (does it even make sense to call a member function without an instance?) 2. The return value of opAssign is `S*`, so it would seem that `t` is assigned a pointer value? 3. What is `tmp`, just another stack allocated instance of S? 4. What is the syntax for explicitly calling the destructor? (In C++, it is tmp.~S(), but in D would it be tmp.~this() or what?)
Oct 21 2011
parent Jesse Phillips <jessekphillips+d gmail.com> writes:
On Sat, 22 Oct 2011 03:21:09 +0000, Sean Silva wrote:

 I'm struggling with this on 4 fronts:
 
 1. What is `this`, when opAssign is called off of the type? (does it
 even make sense to call a member function without an instance?)
Please file a bug on http://d.puremagic.com/issues/ The example doesn't even compile: Error: cannot implicitly convert expression (this) of type S to S*
 2. The return value of opAssign is `S*`, so it would seem that `t` is
 assigned a pointer value?
The correct information is: t=s is defined to be semantically equivalent to: t.opAssign(s) which means a signature of just void opAssign(ref const S rhs) { //... }
 3. What is `tmp`, just another stack allocated instance of S?
I think it is just an example of something you might do.
 4. What is the syntax for explicitly calling the destructor? (In C++, it
 is tmp.~S(), but in D would it be tmp.~this() or what?)
You don't. If you want to destroy use, clear(s);
Oct 22 2011