令人困惑的 C++涉及 *this? 的代码

发布于 2024-11-29 05:48:32 字数 210 浏览 1 评论 0原文

有人可以解释一下这段代码吗?我不明白第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沦落红尘 2024-12-06 05:48:32

此代码:

MyString ss(*this);

表示“声明一个名为 ssMyString 类型的新变量,并将其初始化为 *this 的副本。”在成员函数内部,this 是指向接收者对象(成员函数所作用的对象)的指针,因此 *this 是对接收者对象的引用。因此,您可以将其理解为“创建一个名为 ss 的新 MyString,它是接收者对象的副本。”

这里使用的习惯用法是根据operator += 来实现operator +。这个想法是创建接收者对象的副本,使用operator +=将参数添加到副本中,然后返回副本。这是一个广泛使用的技巧,在给定相应复合赋值运算符的实现的情况下,可以简化独立运算符的实现。

希望这有帮助!

This code:

MyString ss(*this);

Says "declare a new variable of type MyString that's named ss, 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 new MyString that's called ss and is a copy of the receiver object."

The idiom being used here is implementing operator + in terms of operator +=. The idea is to make a copy of the receiver object, use operator += 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!

对不⑦ 2024-12-06 05:48:32

这是用一个运算符实现另一个运算符的通用代码重用技术的一个实例。

假设我们已经定义了一个复合加运算符:

class X
{
  X & operator+=(const X&);
};

这个一元运算符允许我们编写a += b,它修改a并返回对其自身的引用。这一切都很好。现在,如果我们还想提供一个复制、二进制加运算符 a + b,它按值返回新值,并留下 ab code> 不变,那么我们想要利用我们已经为复合运算符编写的代码。我们通过在 a临时副本上调用一元运算符来实现此目的:

X X::operator+(const X & b) const { return X(*this) += b; }
                                           ^^^^^^^^
                                           temporary copy

这正是您的代码所做的,只是更冗长一些。您也可以编写 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:

class X
{
  X & operator+=(const X&);
};

This unary operator allows us to write a += b, it modifies a and returns a reference to itself. This is all fine and good. Now if we also want to provide a copying, binary plus opearator a + b, which returns a the new value by value and leaves both a and b 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 of a:

X X::operator+(const X & b) const { return X(*this) += b; }
                                           ^^^^^^^^
                                           temporary copy

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.

夏天碎花小短裙 2024-12-06 05:48:32

它是 MyString 的构造函数,将当前对象(类型为 MyString)的值作为其参数。

It's a constructor for MyString that takes the value of the current object (of type MyString) as its argument.

浅笑轻吟梦一曲 2024-12-06 05:48:32

据我所知,这是一个复制构造函数,它使用当前 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.

滿滿的愛 2024-12-06 05:48:32

ss 是一个新字符串,它是使用原始字符串的复制构造函数构造的。

ss is a new string which gets constructed with a copy constructor from the original string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文