【问题标题】:The meaning of "*this" in C++ [duplicate]C ++中“*this”的含义[重复]
【发布时间】:2021-04-29 22:01:40
【问题描述】:
vecteur& vecteur::operator+=(const vecteur& autre)
{
    x += autre.x;
    y += autre.y;
    z += autre.z;
    return *this;
}

*this 在这种情况下是什么意思?

有没有可能以不同的方式称呼它,比如简单地return vecteur

【问题讨论】:

  • vecteur 是一个类型,*this 是一个值
  • vecteur 是一个类型,所以不能返回。 (如果你有 2 个 vecteur 变量的图像;这样的语句会返回哪个?)
  • Handy documentation on this 因为事实证明this 有微妙之处,而且边缘有一些怪癖。

标签: c++ pointers this


【解决方案1】:

this 是指向正在调用该方法的对象的指针。 *this 是被取消引用的指针(即访问实际对象本身)。

【讨论】:

    猜你喜欢
    • 2014-04-15
    • 2016-01-23
    • 2010-11-03
    • 2022-08-03
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多