C++面向对象程序设计之this指针

    技术2025-11-04  25

    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指针。

    Processed: 0.057, SQL: 12