关于“这个”的问题c++ 中的指针
我已经被赋予了一个私有 int 变量 x 和 y 的类,以及一个运算符重载函数,
class Bag{
private:
int x;
int y;
public:
Bag();
~Bag();
//.......
//.....etc
};
Bag operator+ (Bag new) const{
Bag result(*this); //what does this mean?
result.x += new.x;
result.y += new.y;
}
“Bag result(*this);”的效果是什么?那里?。
i have been given class with int variables x and y in private, and an operator overload function,
class Bag{
private:
int x;
int y;
public:
Bag();
~Bag();
//.......
//.....etc
};
Bag operator+ (Bag new) const{
Bag result(*this); //what does this mean?
result.x += new.x;
result.y += new.y;
}
What is the effect of having "Bag result(*this);" there?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Bag result(*this)
创建调用运算符函数的对象的副本。例如,如果有:
那么
result
将是op1
的副本。由于operator+函数对其操作数求和并返回总和,因此我们需要一种通过this指针访问操作数op1的方法。
或者我们可以这样做:
Bag result(*this)
creates a copy of the object on which the operator function was called.Example if there was:
then
result
will be a copy ofop1
.Since the
operator+
function is doing a sum of its operands and returning the sum, we need a way to access the operand op1 which is done through thethis
pointer.Alternatively we could have done:
您的代码如下所示:
成员函数的隐式“当前对象”由名为 this 的特殊值指向。然后
*this
获取该对象(通过取消引用 this),并使用它(通过复制构造函数)构造另一个名为 result 的 Bag。我怀疑此代码是从作业中获取的,因此您可能无法使用
Your code would look like:
The implicit "current object" for member functions is pointed to by a special value named this. Then
*this
gets that object (by dereferencing this), and it is used to construct (via the copy constructor) another Bag named result.I suspect this code is taken from a homework assignment, so you might not be able to use the one true addition operator pattern, but it is common and you should be aware of it:
首先,告诉代码编写者不要使用 new 作为变量名——它是一个关键字。另外,请记住
返回结果;
。要么通过 const-reference 传递,要么直接修改 new 包。在结构/类中,
this
是指向自身的指针。因此,*this
是对整个 Bag 实例本身的引用。语句
Bag result(a_bag_reference)
将调用Bag
的复制构造函数,该构造函数将a_bag_reference
复制到result
。因此,
复制自身,然后存储到
结果
中。这使得接下来的 2 个语句不会影响实例本身(即
this->x
和this->y
保持不变)。Firstly, tell the code writer not to use
new
as the variable name — it's a keyword. Also, remeber toreturn result;
. And either pass by const-reference or directly modify thenew
bag.Inside a struct/class,
this
is a pointer to itself. Therefore,*this
is a reference to the whole Bag instance itself.The statement
Bag result(a_bag_reference)
will call the copy constructor ofBag
, which makes a copy ofa_bag_reference
intoresult
.Therefore,
makes a copy of itself, then store into
result
. This makes the next 2 statementsdo not affect the instance itself (i.e.
this->x
andthis->y
are kept constant).operator+
函数返回一个副本。语句:正在制作 this 对象的副本以返回给调用者。
根据签名,它必须返回一个值,因此它正在制作一个副本,然后添加
new
对象。The
operator+
function returns a copy. The statement:Is making a copy of this object to return to the caller.
According to the signature, it must return a value, so it is making a copy and then adding the
new
object.Bag result(*this);
声明一个变量result
并调用其复制构造函数。您可以想象 C++ 自动为所有类声明一个默认的复制构造函数。它的工作只是使用另一个对象初始化一个对象:
表达式
*this
可能看起来有点令人不安,但这正是 C++ 处理&
时常见的恐怖现象。参数。Bag result(*this);
is declaring a variableresult
and invoking its copy constructor.You can imagine C++ automatically declares a default copy constructor for all classes. Its job is simply to initialize an object using another object:
The expression
*this
may look a little unsettling, but is just the usual horror of C++ when you deal with&
parameters.