gpt4 book ai didi

c++ - 谁能详细告诉我 "*this pointer"?

转载 作者:行者123 更新时间:2023-11-30 00:56:28 25 4
gpt4 key购买 nike

例如,在这个中,我对这个指针不是很了解,这个问题不是关于 operator+ 重载,是关于this指针,以及它与类之间的关系,

*这个指针指向什么东西?

非常感谢!

 class Integer {
int i;
public:
Integer(int ii): i(ii) { }
const Integer operator+(const Integer& rv) const {
cout<<"operator+"<<endl;
}

Integer&
operator+=(const Integer& rv) {
i += rv.i;
return *this;
}

}

最佳答案

this标识了一种特殊类型的指针。如果您创建了类 A 的名为 x 的对象,并且类 A 具有非静态成员函数 f()。如果调用函数 x.f(),f() 函数体中的关键字 this 存储了 x 的地址。您不能声明 this 指针或对其进行赋值。基本上,this 指针作为隐藏参数传递给所有非静态成员函数调用,并且在所有非静态函数的主体中作为局部变量可用。

关于c++ - 谁能详细告诉我 "*this pointer"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10053473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com