The operator= of the previously called object is to do so
void Swap (a& x) { Std::swap (A, x.a); } Aoperator= (const a& a) { = A; Swap (temp); return *this; }
The code above looks a bit cumbersome, but it's a good idea.
We can make our swap special in Std.
classa{Private: intA; Public: voidSwap (a& x)//prevent written friend, we give him an interface{Std::swap (A, x.a); } A (intx): A (x) {} A&operator=(Consta&a) {a temp=A; usingStd::swap; Swap (* This, temp); return* This; } voiddisplay () {cout<< a <<Endl; }};namespacestd{//template<> The book, but I found that I could not voidSwap<a> (a &s1, A &s2)//This <A> is a special, and heavy-duty parted {S1.swap (S2); }}intMain () {A A (1); A B (2); B.display (); b=A; B.display ();}
When class is a template, then again.
Reading notes _EFFECTIVE_C++_ clause 25: Consider writing out a swap function that does not throw an exception