tmoran@acm.org wrote in message news:... > > classical idiom in C++ is to copy construct a temporary from the > > right hand side (the anonymous object which is created), then swap > > all of the fields with the target object, so that the anonymous > > object has the bit pattern of the original object, and vice versa. > > On leaving the > >... > >Because of the swap, C++ gets by with one finalization less:-). On the > What if the object is on a linked list with back pointers so that > after the swap the pointers are wrong? Or if it just contains pointers > to internal parts of itself? It's up to the programmer to make the swap work correctly. Most of the time, it isn't a problem -- nobody should have any pointers to the temporary object in function, for example. As for the linked list example, I would imagine that the swap function shouldn't touch the pointers involved. The target object remains linked where it was, regardless of the assignment. Obviously, this idiom doesn't work well with objects that, for example, register themselves somewhere (i.e. the constructor "publishes" the objects addess). But if my experience is typical, such objects have identity, and shouldn't be assigned or copied anyway. About a third of my C++ classes don't allow assignment at all, for such reasons. -- James Kanze GABI Software mailto:kanze@gabi-soft.fr Conseils en informatique orient�e objet/ Beratung in objektorientierter Datenverarbeitung 11 rue de Rambouillet, 78460 Chevreuse, France, T�l. : +33 (0)1 30 23 45 16 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]