提问人:J.Doe 提问时间:10/17/2022 更新时间:10/17/2022 访问量:201
具有同一对象的 C++ 复制分配
C++ Copy assignment with same object
问:
在著名的C++入门书中,我找到了以下代码片段
Message& Message::operator=(const Message &rhs)
{
// handle self-assignment by removing pointer before inserting them
remove_from_Folders(); // updating existing Folders
contents = rhs.contents; // copy message contents from rhs
folders = rhs.folders; // copy Folder pointers from rhs
add_to_Folders(rhs); // add this Message to those Folders
return *this;
}
Message 类:
class Message {
friend class Folder;
public:
// folders is implicity initialized to the empty set
explicit Message(const std::string &str = ""):
contents(str) { }
// copy control to manage pointers to this Message
Message(const Message&); // copy constructor
Message& operator=(const Message&) // copy assignment
~Message(); // destructor
// add/remove this Message from the specified Folder's set of messages
void save(Folder&);
void remove(Folder&);
private:
std::string contents; // actual message text
std::set<Folder*> folders; // Folder's that have this message
// utility functions used by copy constructor, assignment, and destructor
// add this Message to the Folders that point to the parameter
void add_to_Folders(const Message&);
// remove this Message from every Folder in folders
void remove_from_Folders();
}
void Message::save(Folder &f)
{
folders.insert(&f); // add the given Folder to our list of Folders
f.addMsg(this); // add this Message to f's set of Messages
}
void Message::remove(Folder &f)
{
folders.erase(&f); // take the given Folder out of our list of Folders
f.remMsg(this); // remove this Message to f's set of Messages
}
// add this Message to Folders that point to m
void Message::add_to_Folders(const Message &m)
{
for (auto f : m.folders) // for each Folder that holds m
f->addMsg(this); // add a pointer to this Message to that Folder
}
void Message::remove_from_Folders()
{
for (auto f : folders) // for each pointer in folders
f->remMsg(this); // remove this Message from that Folder
folders.clear(); // no Folder points to this Message
}
Message::~Message()
{
remove_from_Folders();
}
现在我的问题是:我们看到整个文件夹集被清除了。我们还看到 copy-assignment 运算符中的参数是一个引用。那么,为什么召唤它,然后做它,在这里起作用呢?既然是引用,那么当我们使用同一个对象(即进行自我(复制)赋值)时,现在不会是空集吗?这不就是引用的工作方式吗?或者编译器在使用赋值运算符进行操作时是否满足了特殊规则?remove_from_Folders
rhs
remove_from_Folders()
folders = rhs.folders;
rhs
rhs.folders
答: 暂无答案
评论
Folder
folders
if (&rhs == this) return;
assert
)assert
abort
abort