Return 运算符重载中的 this 或 *this?
Return this or *this in operator overloading?
我正在尝试重载 C++ 中的运算符。为此,我写了下面的代码:
#include <iostream>
using namespace std;
class Box
{
public:
int height;
int width;
int length;
public:
Box operator+(const Box& b)
{
this->length = this->length + b.length;
this->width = this->width + b.width;
this->height = this->height + b.height;
return *this;
}
};
int main()
{
Box b1,b2;
b1.length = 5;
b1.width = 5;
b1.height = 5;
cout << "Length is : " << b1.length;
cout << "width is : " << b1.width;
cout << "height is : " << b1.height;
b2.length = 5;
b2.width = 5;
b2.height = 5;
b1 = b1 + b2 ;
cout << "Length is : " << b1.length;
cout << "width is : " << b1.width;
cout << "height is : " << b1.height;
cout << "Hello from first c++";
return 0;
}
主要部分是:
Box operator+(const Box& b)
{
this->length = this->length + b.length;
this->width = this->width + b.width;
this->height = this->height + b.height;
return *this;
}
我听不懂:
this->length = this->length + b.length;
return *this;
this.length 在这里不起作用。
我为什么要return *this
? return this
这里还不够吗?
只要您 return 引用或复制,您就想 return *this
,并且 this
只有当您 return 指向 class 对象(你几乎永远不会)。
- "this"是指针
- "this"指针是一个常量指针,保存当前对象的内存地址
会员签名:
Box operator+(const Box& b)
why should I return *this? Is return this not enough here?
如果您return输入this
,那么您将return输入一个指针Box*
,而您的会员签名不同意。
因此,您需要 return 按值,因此取消引用它和 return。
"this" 是指向 Box 类型对象的指针。
您的 return 类型是 Box 类型的具体对象。
当你 return *this 时,你正在取消引用指针并 return 引用你的对象 (Box&) 然后你的编译器使用复制构造函数 Box( const Box&).
事实上,如果您编写 b3 = b1+b2,您会注意到修改 b3 不会进一步修改 b1。
关于你的问题的字母。
附带说明一下,您在定义运算符的方式上有些混乱。通常当你重载算术运算符时,你可以同时重载 operator+ 和 operator+=
operator+ return 是给定类型的新具体对象,不应修改操作数。它的典型签名是:
Box Box::operator+(const Box& R) const
所以实现它的典型方法是复制两个操作数之一,然后求和 return 这个新对象。请注意,该函数被声明为常量
如果您想按照您想要的方式修改第一个操作数,请使用 += 运算符。
该运算符具有不同的签名,它假设您修改了操作数,在这种情况下,它实际上是 return 一个引用,应该是对左操作数的引用。所以在这种情况下你将有
Box& Box::operator+=(const Box& R)
在这种情况下,您可以看到该函数不是常量,因为您假设要修改操作数,在这种情况下,您应该 return *this 这是对您刚刚修改的对象的引用.
(撇开 operator+()
更改 this
的值这一事实)你 return *this
的原因是允许这样的表达式:
Box a, b, c;
// ... init the Boxes
Box d = a + b + c;
在这种情况下,a+b
的结果需要 "fed" 到 operator+
以将 c
的值添加到其中。这是通过创建一个表示 a+b
结果的新临时对象来完成的(在您的情况下 - 这是 return 在您的 operator+
中编辑为 *this
的内容)。
现在你应该看到,如果你要 return 一个指针 this
那么就没有办法优雅地写下 a+b+c
作为 [=15 的结果=] 将不再是参考,为了正确调用 operator+
它会写一些丑陋的东西,如:
(a+b).operator+(c)
上述表达式中的赋值 operator=
也是同样的道理——您确实想要 return 对象的引用,而不是指向它的指针。
我正在尝试重载 C++ 中的运算符。为此,我写了下面的代码:
#include <iostream>
using namespace std;
class Box
{
public:
int height;
int width;
int length;
public:
Box operator+(const Box& b)
{
this->length = this->length + b.length;
this->width = this->width + b.width;
this->height = this->height + b.height;
return *this;
}
};
int main()
{
Box b1,b2;
b1.length = 5;
b1.width = 5;
b1.height = 5;
cout << "Length is : " << b1.length;
cout << "width is : " << b1.width;
cout << "height is : " << b1.height;
b2.length = 5;
b2.width = 5;
b2.height = 5;
b1 = b1 + b2 ;
cout << "Length is : " << b1.length;
cout << "width is : " << b1.width;
cout << "height is : " << b1.height;
cout << "Hello from first c++";
return 0;
}
主要部分是:
Box operator+(const Box& b)
{
this->length = this->length + b.length;
this->width = this->width + b.width;
this->height = this->height + b.height;
return *this;
}
我听不懂:
this->length = this->length + b.length;
return *this;
this.length 在这里不起作用。
我为什么要return *this
? return this
这里还不够吗?
只要您 return 引用或复制,您就想 return *this
,并且 this
只有当您 return 指向 class 对象(你几乎永远不会)。
- "this"是指针
- "this"指针是一个常量指针,保存当前对象的内存地址
会员签名:
Box operator+(const Box& b)
why should I return *this? Is return this not enough here?
如果您return输入this
,那么您将return输入一个指针Box*
,而您的会员签名不同意。
因此,您需要 return 按值,因此取消引用它和 return。
"this" 是指向 Box 类型对象的指针。 您的 return 类型是 Box 类型的具体对象。
当你 return *this 时,你正在取消引用指针并 return 引用你的对象 (Box&) 然后你的编译器使用复制构造函数 Box( const Box&).
事实上,如果您编写 b3 = b1+b2,您会注意到修改 b3 不会进一步修改 b1。 关于你的问题的字母。
附带说明一下,您在定义运算符的方式上有些混乱。通常当你重载算术运算符时,你可以同时重载 operator+ 和 operator+=
operator+ return 是给定类型的新具体对象,不应修改操作数。它的典型签名是:
Box Box::operator+(const Box& R) const
所以实现它的典型方法是复制两个操作数之一,然后求和 return 这个新对象。请注意,该函数被声明为常量
如果您想按照您想要的方式修改第一个操作数,请使用 += 运算符。 该运算符具有不同的签名,它假设您修改了操作数,在这种情况下,它实际上是 return 一个引用,应该是对左操作数的引用。所以在这种情况下你将有
Box& Box::operator+=(const Box& R)
在这种情况下,您可以看到该函数不是常量,因为您假设要修改操作数,在这种情况下,您应该 return *this 这是对您刚刚修改的对象的引用.
(撇开 operator+()
更改 this
的值这一事实)你 return *this
的原因是允许这样的表达式:
Box a, b, c;
// ... init the Boxes
Box d = a + b + c;
在这种情况下,a+b
的结果需要 "fed" 到 operator+
以将 c
的值添加到其中。这是通过创建一个表示 a+b
结果的新临时对象来完成的(在您的情况下 - 这是 return 在您的 operator+
中编辑为 *this
的内容)。
现在你应该看到,如果你要 return 一个指针 this
那么就没有办法优雅地写下 a+b+c
作为 [=15 的结果=] 将不再是参考,为了正确调用 operator+
它会写一些丑陋的东西,如:
(a+b).operator+(c)
上述表达式中的赋值 operator=
也是同样的道理——您确实想要 return 对象的引用,而不是指向它的指针。