In C + +, it is well known that in a resource management class (such as a pointer to heap memory), you need to redefine the copy constructor, assignment operator, and destructor (Big Three), and you may need to define the move constructor and the move assignment operator under the new standard (big Five). But in fact, this rule can also have a small extension. In resource management classes, it is often necessary to redefine their own swap functions as a means of optimization .
1. Swap function
First, consider the following example, assuming that the class hasptr contains a pointer to string *ps and an int type value.
class hasptr { public: ... ... ... Private : string* PS; int value;};
If you do not define your own swap function, calling the standard library's std::swap is equivalent to doing the following:
Hasptr temp ==
The string in V1 here has been copied two times, resulting in unnecessary waste of efficiency .
In fact, we know that swap is just a pointer to the swap, so we can write the following custom version of the Swap function:
class hasptr { void swap (hasptr& lhs, hasptr& RHS); ... ...}; inlinevoid swap (hasptr& lhs, hasptr& rhs) { using std::swap; Swap (lhs.ps, rhs,ps); Swap (Lhs.value, rhs.value);}
Note: One thing to note here is that the function call should be written as swap, rather than std::swap form. This is because if Hasptr contains member Foo of a particular type (such as Foo type), then if Foo defines its swap function, it should call its custom swap function, otherwise call the Std::swap of the standard library.
So the following form 1 is wrong and should be written as shown in Form 2.
// Form 1 void Swap (hasptr& lhs, hasptr& rhs) { std::swap (Lhs.foo, Rhs.foo);} // Form 2 void Swap (hasptr& lhs, hasptr& rhs) { using std::swap; Swap (Lhs.foo, rhs.foo);}
2. Copy and Swap idiom
Classes that define swap functions commonly use swap to define their assignment operators , the copy and swap technique.
This can be done by natural abnormal security and the correct handling of self-assignment problem, is a concise and efficient way of writing. (Why other methods have exception security or self-assignment issues can participate in effective C + + clause 11)
The above example uses the copy and swap technique to define the assignment operator as follows:
hasptr& hasptr::operator=(hasptr rhs) { swap (*this, RHS); return *this;}
It can be seen that, unlike the general assignment operator, its arguments are in the way of passing values , not references. It is functionally equivalent to the following code, but is more efficient (moves the copy action to the function parameter construction phase)
hasptr& hasptr::operator= (const hasptr& RHS) { hasptr temp (RHS); Swap (*this, temp); return *this;}
Both of these methods can perfectly solve the problem of abnormal security and self-assignment. The first method is recommended by the C + + primer and C + + programming specification, and Scott Meyers expresses some concerns about its readability in effective C + +, but if you master the copy and swap idiom, it is recommended to generate more efficient code with the first notation.
Resources:
1.Stanley B. lippman/josée Lajoie/barbara E. Moo, C + + Primer Chinese version (5th edition) [M]. Electronic industry Press, 2013
2.Meyers S. Effective C++[m]. Addison Wesley, 2005.
3.Sutter H, Alexandrescu A. C + + Coding standards:101 Rules, guidelines, and best practices (C + + in Depth Series) [M]. Addison-wesley Professional, 2004.
4. Answers to the question about swap and copy idiom on StackOverflow: Http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom
Swap function & Copy-and-swap idiom