为什么复制构造函数不需要检查输入对象是否指向自身?

Why does copy constructor not need to check whether the input object pointers to itself or not?

本文关键字:是否 对象 构造函数 复制 不需要 检查 输入 为什么      更新时间:2023-10-16

正如下面的代码,复制赋值运算符必须检查输入对象是否指向自身。我想知道为什么复制构造函数不需要做同样的检查。

我是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似乎允许这样做,只要您不访问尚未构造的对象本身。允许使用&获取其地址。

假设你有像冲锋队这样的简单对象:

class Stormtrooper
{
char* cstring; // raw pointer used as a handle to a dynamically-allocated memory block
void clone(const char* s)
{
std::size_t n = std::strlen(s) + 1;
cstring = new char[n];
std::memcpy(cstring, s, n); // populate
}
public:
Stormtrooper(const char* s = "I am a Stormtrooper clone") { clone(s); }
~Stormtrooper()
{
delete[] cstring;  // deallocate
}
Stormtrooper(const Stormtrooper& other) // copy constructor
{ 
clone(other.cstring);
}
Stormtrooper& operator=(const Stormtrooper& other) // copy assignment
{
if(this != &other) {
delete[] cstring;  // deallocate
clone(other.cstring);
}
return *this;
}
};

如果您想将冲锋队分配给另一个冲锋队,检查两个冲锋队是否已经相同(通常是相同的(很有用。通过这种方式,您可以避免 clone(( 操作,因为冲锋队已经相同了。

但是如果你想创建一个新的冲锋队,并且你希望他与另一个冲锋队相同(像往常一样(,你可以复制构造它,在这种情况下,clone(( 操作将正确执行。

通过这种方式,您可以非常轻松地创建整个冲锋队军队。