[英]How do I properly overload a operator= when creating a copy constructor
I was having a lecture at my University for C++, and I couldn't really understand what the operator= is supposed to do and how to overload it properly.我在我的大学讲授 C++,我无法真正理解 operator= 应该做什么以及如何正确重载它。 In almost all examples from lectures that i could find, the part of the code for the copy and move constructor would look like this:
在我能找到的几乎所有讲座示例中,复制和移动构造函数的代码部分如下所示:
class MyClass {
MyClass(const MyClass& s) {
copy(s); // the function is supposed to do the copying.
}
MyClass& operator=(const MyClass& s){
if(this != &s){ remove(); copy(s);} // the remove function is defined later on in the code, and it
return *this;} deletes a given pointer.
Now my question is, why is there an if statement her and what it does it do, and is there another way to properly overload the operator=, and why are we returning a class reference in the operator overload?现在我的问题是,为什么有一个 if 语句 her 以及它的作用是什么,还有另一种方法可以正确重载 operator=,为什么我们要在运算符重载中返回类引用? Would be a mistake if we return it by value and not by reference?
如果我们按值而不是按引用返回它会出错吗? Also when is it a bad idea to copy things because I have read a lot about how copying can lead to memory leak, but i've never seen such an incident so I don't know what to make of it.
另外,什么时候复制东西是一个坏主意,因为我已经阅读了很多关于复制如何导致内存泄漏的内容,但我从未见过这样的事件,所以我不知道该怎么办。 Any help would be gladly appreciated!
任何帮助将不胜感激!
This has nothing to do, whatsoever, with copy constructors.无论如何,这与复制构造函数无关。
This is checking for an edge case when an object gets assigned to itself:这是在对象被分配给自身时检查边缘情况:
MyClass c;
// Some code
c=c;
c=c
is perfectly valid C++. c=c
是完全有效的 C++。 You can assign an object to itself.您可以将对象分配给自身。 Presumably, you expect this to do nothing.
据推测,您希望这不会做任何事情。 This object doesn't change.
这个对象不会改变。
The comparison to this
in the assignment operator is done when the assignment operator itself would break horribly if the copy constructor gets called for self-assignment.如果调用复制构造函数进行自赋值,则赋值运算符本身会严重中断时,会在赋值运算符中与
this
进行比较。 If, in this case, s
is the same object as this
.如果在这种情况下,
s
与this
是同一个对象。
There is no law that says that assignment operator must make this comparison.没有法律规定赋值运算符必须进行这种比较。 Many assignment operator overloads work perfectly fine, as is, even when assigning the object to itself.
许多赋值运算符重载工作得非常好,即使在将对象分配给自身时也是如此。
But whatever this assignment operator does, it won't work if the assignment is from itself.但是无论这个赋值运算符做什么,如果赋值来自它本身,它就不起作用。 You failed to show what this assignment operator does, but the fact that it calls something called
remove
is a big honking clue.你没有展示这个赋值运算符的作用,但它调用了一个叫做
remove
东西这一事实是一个很大的提示。 It removes something from the object, so without this check, assigning this object to itself would, otherwise, make it completely "empty".它从对象中删除了一些东西,所以如果没有这个检查,把这个对象分配给它自己,否则,使它完全“空”。 Which would be a rather rude thing to do, wouldn't you agree?
这将是一件相当粗鲁的事情,你不同意吗?
There was once an outdated idiom called Rule of Big 3 .曾经有一个过时的成语叫做三巨头规则。 The recommended implementation of that rule was another outdated Copy+Swap Idiom .
该规则的推荐实现是另一个过时的Copy+Swap Idiom 。 Those idioms make good starting points for beginner C++ programmers.
这些习语为初学者 C++ 程序员提供了很好的起点。 Later you may find out better trade-offs and optimized alternates for special cases.
稍后您可能会发现针对特殊情况的更好的权衡和优化的替代方案。 For now, sticking to the old idioms - at least - can improve guarantees for functionality and correctness of your code.
目前,坚持旧的习惯用法 - 至少 - 可以提高对代码功能和正确性的保证。
cheers, FM.干杯,FM。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.