关于“这个”的问题c++ 中的指针

发布于 2024-08-27 01:01:38 字数 357 浏览 5 评论 0原文

我已经被赋予了一个私有 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 技术交流群。

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

发布评论

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

评论(5

傲鸠 2024-09-03 01:01:38

Bag result(*this) 创建调用运算符函数的对象的副本。

例如,如果有:

sum = op1 + op2; 

那么 result 将是 op1 的副本。

由于operator+函数对其操作数求和并返回总和,因此我们需要一种通过this指针访问操作数op1的方法。

或者我们可以这样做:

Bag result;
result.x = (*this).x + newobj.x; // note you are using new which is a keyword.
result.y = (*this).y + newobj.y; // can also do this->y instead
return result;

Bag result(*this) creates a copy of the object on which the operator function was called.

Example if there was:

sum = op1 + op2; 

then result will be a copy of op1.

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 the this pointer.

Alternatively we could have done:

Bag result;
result.x = (*this).x + newobj.x; // note you are using new which is a keyword.
result.y = (*this).y + newobj.y; // can also do this->y instead
return result;
护你周全 2024-09-03 01:01:38

您的代码如下所示:

class Bag {
public:
  Bag();
  Bag(Bag const& other); // copy ctor, declared implicitly if you don't declare it
  ~Bag();

  Bag operator+(Bag const& other) const;

private:
  int x;
  int y;
};

Bag Bag::operator+(Bag const& other) const {
  Bag result (*this);
  result.x += other.x;         
  result.y += other.y;
  return result;
}

成员函数的隐式“当前对象”由名为 this 的特殊值指向。然后 *this 获取该对象(通过取消引用 this),并使用它(通过复制构造函数)构造另一个名为 result 的 Bag。

我怀疑此代码是从作业中获取的,因此您可能无法使用

struct Bag {
  //...
  Bag& operator+=(Bag const& other) {
    x += other.x;
    y += other.y;
    return *this; // return a reference to the "current object"
    // as almost all operator=, operator+=, etc. should do
  }
};

Bag operator+(Bag a, Bag const& b) {
  // notice a is passed by value, so it's a copy
  a += b;
  return a;
}

Your code would look like:

class Bag {
public:
  Bag();
  Bag(Bag const& other); // copy ctor, declared implicitly if you don't declare it
  ~Bag();

  Bag operator+(Bag const& other) const;

private:
  int x;
  int y;
};

Bag Bag::operator+(Bag const& other) const {
  Bag result (*this);
  result.x += other.x;         
  result.y += other.y;
  return result;
}

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:

struct Bag {
  //...
  Bag& operator+=(Bag const& other) {
    x += other.x;
    y += other.y;
    return *this; // return a reference to the "current object"
    // as almost all operator=, operator+=, etc. should do
  }
};

Bag operator+(Bag a, Bag const& b) {
  // notice a is passed by value, so it's a copy
  a += b;
  return a;
}
何其悲哀 2024-09-03 01:01:38

首先,告诉代码编写者不要使用 new 作为变量名——它是一个关键字。另外,请记住返回结果;。要么通过 const-reference 传递,要么直接修改 new 包。


在结构/类中,this 是指向自身的指针。因此,*this 是对整个 Bag 实例本身的引用。

语句 Bag result(a_bag_reference) 将调用 Bag 的复制构造函数,该构造函数将 a_bag_reference 复制到 result

因此,

Bag result(*this);

复制自身,然后存储到结果中。这使得接下来的 2 个语句

result.x += new.x;
result.y += new.y;

不会影响实例本身(即 this->xthis->y 保持不变)。

Firstly, tell the code writer not to use new as the variable name — it's a keyword. Also, remeber to return result;. And either pass by const-reference or directly modify the new 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 of Bag, which makes a copy of a_bag_reference into result.

Therefore,

Bag result(*this);

makes a copy of itself, then store into result. This makes the next 2 statements

result.x += new.x;
result.y += new.y;

do not affect the instance itself (i.e. this->x and this->y are kept constant).

一绘本一梦想 2024-09-03 01:01:38

operator+ 函数返回一个副本。语句:

Bag result(*this);

正在制作 this 对象的副本以返回给调用者。
根据签名,它必须返回一个值,因此它正在制作一个副本,然后添加 new 对象。

The operator+ function returns a copy. The statement:

Bag result(*this);

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.

一笑百媚生 2024-09-03 01:01:38

Bag result(*this); 声明一个变量 result 并调用其复制构造函数

您可以想象 C++ 自动为所有类声明一个默认的复制构造函数。它的工作只是使用另一个对象初始化一个对象:

Bag::Bag(Bag const& src) {
   x = src.x;
   y = src.y;
}

表达式 *this 可能看起来有点令人不安,但这正是 C++ 处理 & 时常见的恐怖现象。参数。

Bag result(*this); is declaring a variable result 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:

Bag::Bag(Bag const& src) {
   x = src.x;
   y = src.y;
}

The expression *this may look a little unsettling, but is just the usual horror of C++ when you deal with & parameters.

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