令人困惑的 C++涉及 *this? 的代码
有人可以解释一下这段代码吗?我不明白第 3 行:
MyString MyString::operator+(const MyString &str)
{
MyString ss(*this); //----> explain this part
ss += str;
return ss;
}
谢谢!
Could someone explain this code? I don't understand line 3:
MyString MyString::operator+(const MyString &str)
{
MyString ss(*this); //----> explain this part
ss += str;
return ss;
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此代码:
表示“声明一个名为
ss
的MyString
类型的新变量,并将其初始化为*this
的副本。”在成员函数内部,this
是指向接收者对象(成员函数所作用的对象)的指针,因此*this
是对接收者对象的引用。因此,您可以将其理解为“创建一个名为ss
的新MyString
,它是接收者对象的副本。”这里使用的习惯用法是根据
operator +=
来实现operator +
。这个想法是创建接收者对象的副本,使用operator +=
将参数添加到副本中,然后返回副本。这是一个广泛使用的技巧,在给定相应复合赋值运算符的实现的情况下,可以简化独立运算符的实现。希望这有帮助!
This code:
Says "declare a new variable of type
MyString
that's namedss
, and initialize it as a copy of*this
." Inside of a member function,this
is a pointer to the receiver object (the object that the member function is acting on), so*this
is a reference to the receiver object. Consequently, you can read this as "make a newMyString
that's calledss
and is a copy of the receiver object."The idiom being used here is implementing
operator +
in terms ofoperator +=
. The idea is to make a copy of the receiver object, useoperator +=
to add the parameter to the copy, and then to return the copy. It's a widely-used trick that simplifies the implementation of freestanding operators given implementation of the corresponding compound assignment operator.Hope this helps!
这是用一个运算符实现另一个运算符的通用代码重用技术的一个实例。
假设我们已经定义了一个复合加运算符:
这个一元运算符允许我们编写
a += b
,它修改a
并返回对其自身的引用。这一切都很好。现在,如果我们还想提供一个复制、二进制加运算符a + b
,它按值返回新值,并留下a
和b
code> 不变,那么我们想要利用我们已经为复合运算符编写的代码。我们通过在a
的临时副本上调用一元运算符来实现此目的:这正是您的代码所做的,只是更冗长一些。您也可以编写
return MyString(*this) += str;
还有其他遵循类似精神的习惯用法,例如根据 const 访问实现非常量访问,在中复制分配复制构造和交换方面,移动分配方面移动构造和交换。它总是归结为避免代码重复。
This is an instance of the common code reuse technique to implement one operator in terms of another.
Suppose we have already defined a compound-plus operator:
This unary operator allows us to write
a += b
, it modifiesa
and returns a reference to itself. This is all fine and good. Now if we also want to provide a copying, binary plus opearatora + b
, which returns a the new value by value and leaves botha
andb
unchanged, then we want to take advantage of the code we've already written for the compound operator. We do so by calling the unary operator on a temporary copy ofa
:This is exactly what your code does, only a bit more verbosely. You could as well have written
return MyString(*this) += str;
There are other idioms with follow a similar spirit, such as implementing non-const access in terms of const access, copy-assign in terms of copy-construct and swap, and move-assign in terms of move-construct and swap. It always boils down to avoiding code duplication.
它是
MyString
的构造函数,将当前对象(类型为MyString
)的值作为其参数。It's a constructor for
MyString
that takes the value of the current object (of typeMyString
) as its argument.据我所知,这是一个复制构造函数,它使用当前 MyString 对象的内容创建一个新的 MyString 。
From what I can tell that is a copy constructor which creates a new MyString with the contents of the current MyString object.
ss
是一个新字符串,它是使用原始字符串的复制构造函数构造的。ss
is a new string which gets constructed with a copy constructor from the original string.