<分区>
Possible Duplicate:
How can moved objects be used?
What constitutes a valid state for a “moved from” object in C++11?
在 C++11 中实现 move 语义时,应该将移出的对象保留在安全状态,还是仅保留在“垃圾”状态?
例如在以下 C++11 包装器到原始 FILE*
资源的示例中,实现 move 构造函数的首选选项是什么?
// C++11 wrapper to raw FILE*
class File
{
FILE* m_fp;
public:
// Option #1
File(File&& other)
: m_fp(other.m_fp)
{
// "other" left in a "junk" state
}
// Option #2
File(File&& other)
: m_fp(other.m_fp)
{
// Avoid dangling reference in "other"
other.m_fp = nullptr;
}
...
};