this 指针是什么
this指针是在定义对象的时候,指向该对象的指针
this指针有什么用
this 指针的用途在三个方面 1、构造函数里把对象的给数据成员赋值。如
Human
::Human(const Human
&other
) {
this->a
= other
.a
;
this->b
= other
.b
;
}
2、类的成员函数返回为引用。如
Human
& Human
::test(Human
&other
) {
if (this->a
> other
.a
) {
return *this;
}
}
3、类返回值为指针。如
```cpp
Human
* Human
::test2(Human
*other
) {
if (a
> other
->a
) {
return this;
}
注意:类中静态成员函数不可使用this指针。
转载请注明原文地址:https://ipadbbs.8miu.com/read-60644.html