c++ - 为什么复制构造函数不需要检查输入对象是否指向自身?
问题描述
如下代码所示,复制赋值运算符必须检查输入对象是否指向自身。我想知道为什么复制构造函数不需要做同样的检查。
我是 C++ 的新手。如果能在这个问题上得到一些帮助,我将不胜感激。
class rule_of_three
{
char* cstring; // raw pointer used as a handle to a dynamically-allocated memory block
void init(const char* s)
{
std::size_t n = std::strlen(s) + 1;
cstring = new char[n];
std::memcpy(cstring, s, n); // populate
}
public:
rule_of_three(const char* s = "") { init(s); }
~rule_of_three()
{
delete[] cstring; // deallocate
}
rule_of_three(const rule_of_three& other) // copy constructor
{
init(other.cstring);
}
rule_of_three& operator=(const rule_of_three& other) // copy assignment
{
if(this != &other) {
delete[] cstring; // deallocate
init(other.cstring);
}
return *this;
}
};
解决方案
自分配有时会发生,这是正常使用课程的一部分。
将尚未构造的对象作为参数传递给它自己的复制(或移动)构造函数是不正常的。虽然本身不是未定义的行为1,但没有充分的理由这样做,而且通常不会发生。它可能会意外发生,或者如果有人故意试图破坏您的课程。
因此,传统上复制(和移动)构造函数不检查&other != this
.
但是,如果您想要一些额外的安全性,没有什么能阻止您这样做:
rule_of_three(const rule_of_three& other) // copy constructor
{
assert(&other != this);
init(other.cstring);
}
1 [basic.life]/7
似乎允许这样做,只要您不访问尚未构建的对象本身。&
允许使用它的地址
推荐阅读
- r-markdown - 为什么markdown文件不能识别项目“+”?
- mapbox - 如何在 Mapbox GL map.setFilter 中使用“in”表达式
- ubuntu - 无法选择具有以下功能的设备驱动程序“”:[[gpu]]
- google-apps-script - Google OAuth 同意屏幕验证状态 未发布
- javascript - jquery函数将“@”符号转换为“Q”
- unity3d - 简单激活器菜单中的 Unity 错误 CS1519
- python-3.x - 在 PyCharm 中使用黑色格式化程序的问题
- gulp - Gulp 在构建期间下载远程 JSON 文件
- forms - Symfony 4 - 身份验证 1form 2tables
- c++ - CMake:将微存储库迁移到 monorepo 时的最佳结构?