gpt4 book ai didi

c++ - 移出的对象是否应该保留在 "safe"状态?

转载 作者:可可西里 更新时间:2023-11-01 17:37:12 26 4
gpt4 key购买 nike

<分区>

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;
}

...
};

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com