ZHO*_*HOU 3 c++ copy-constructor
在关于复制和交换的这个问题中,在接受的答案中有一个swap()函数.
friend void swap(dumb_array& first, dumb_array& second) // nothrow
{
// enable ADL (not necessary in our case, but good practice)
using std::swap;
// by swapping the members of two classes,
// the two classes are effectively swapped
swap(first.mSize, second.mSize);
swap(first.mArray, second.mArray);
}
Run Code Online (Sandbox Code Playgroud)
为什么不std::swap(first, second)呢?
std::swap依赖于赋值运算符.因此,如果赋值运算符要调用std::swap,这将导致赋值运算符和赋值运算符之间来回的无限调用std::swap.
关于你标题中的问题.是的,对于具有值语义的行为良好的类,可以安全地调用std::swap该类的对象.由于上述原因,只有类的实现者不能将它用于复制和交换习语.
| 归档时间: |
|
| 查看次数: |
1249 次 |
| 最近记录: |