【问题标题】:question regarding "this" pointer in c++关于c ++中“this”指针的问题
【发布时间】:2011-02-01 16:35:43
【问题描述】:

我已经获得了具有 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);”的效果是什么那里?。

【问题讨论】:

  • operator+ 函数是否缺少 return 语句?
  • 这看起来不像是有效的 C++ -- new is 关键字
  • 如果你想创建运算符,我建议查看Boost.Operators。他们将类似的运算符组合在一起(例如+=+),并且只编写其中一个组可以免费授予您其他组:)

标签: c++ class operator-overloading this


【解决方案1】:

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;

【讨论】:

    【解决方案2】:

    operator+ 函数返回一个副本。声明:

    Bag result(*this);
    

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

    【讨论】:

      【解决方案3】:

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


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

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

      因此,

      Bag result(*this);
      

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

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

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

      【讨论】:

        【解决方案4】:

        您的代码如下所示:

        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。

        我怀疑此代码取自家庭作业,因此您可能无法使用 one true addition operator 模式,但它很常见,您应该注意它:

        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;
        }
        

        【讨论】:

        • 这是一种危险的写 op+() 的方式——第一个参数将被切片,如果打算在基于它的函数中进行任何多态行为,则不会。最好将两个参数都设为 const 引用,并在函数内创建副本。
        • @Neil:在函数内复制也是切片;在需要更改之前,最好使用此模式。另请参阅cpp-next.com/archive/2009/08/want-speed-pass-by-value,它为 operator= 推广相同的成语。
        【解决方案5】:

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

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

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

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

        【讨论】:

          猜你喜欢
          • 2014-05-13
          • 1970-01-01
          • 2023-01-08
          • 2021-10-29
          • 1970-01-01
          • 2022-01-05
          • 2017-08-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多