TITLE: Consistency for operator = and copy constructors PROBLEM: kanze@us-es.sel.de (James Kanze (R.30) As a general rule, I try to have operator= and the copy constructor have the same semantics. It makes for less confusion. I've not seen the following trick documented elsewhere, but it seems to me the best way of ensuring the same semantics. Most of the time, I define the assignment operator as follows: T& operator= (const T& rhs) { this->T::~T () ; // explicitly destroy the previous contents. new (this) T (rhs) ; // and reconstruct with the new. return *this ; } RESPONSE: sdm@cs.brown.edu (Scott Meyers) Whenever you write an assignment operator, please don't forget to test for assignment to self! The above operator will almost always fail when you do this: x = x; It will also fail when you do this, x = y; if x and y happen to be aliases at the time. The easiest way to avoid this problem is to begin your assignment operator with one of the following: T& operator= (const T& rhs) { T& operator= (const T& rhs) { if (this == &rhs) if (*this == rhs) return *this; return *this; ... ... } } For details, see "Effective C++," Item 17.