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指针。
本文深入解析C++中this指针的概念与作用,详细介绍了this指针在构造函数、成员函数引用返回及指针返回值的应用场景,并强调了静态成员函数不可使用this指针的限制。
75万+

被折叠的 条评论
为什么被折叠?



