Can I call the default opAssign after overloading opAssign?

Jonathan M Davis jmdavisProg at gmx.com
Sun Nov 18 21:22:07 PST 2012


On Monday, November 19, 2012 06:01:55 Rob T wrote:
> I assume that when I define an opAssign, only the opAssign that I
> define gets called, which means there's no blit or postblit being
> called ever again.
> 
> I may be thoroughly confused at this point. Is there both a blit
> and a postblit, and an optional opAssign that when specified will
> override both?

postblit constructors and opAssign aren't really related. The postblit 
constructor is used when a _new_ instance is being constructed (it plays the 
same role as a copy constructor in C++). opAssign overloads the assignment 
operator and is only used when the assignment operator is used, which does 
_not_ happen when contstructing a new instance but only when replacing the 
value of an instance with that of another.

S s1;
S s2 = s1; // postblit
s1 = s2; // opAssign
foo(s1); // postblit

If you don't define a postblit constructor, then when a new instance is created 
from another, then the original is memcpyed/blitted to the new one. If you 
_do_ define a postblit constructor, then the original is memcpyed/blitted and 
then _after_ that the postblit constructor is called so that you have the 
opportunity to deep copy the pieces that need to be deep copied.

If you don't define opAssign, then when assigning from the one instance to 
another, a memcpy/blit is done to copy the data over. If you _do_ define a 
opAssign, then no memcpy/blit is made at all, but rather opAssign is called.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list