C++ this指针是什么?怎么用?

文章导读
Previous Quiz Next this 指针 C++ 中的每个对象都通过一个重要的指针称为 this 指针来访问自身的地址。this 指针是所有成员函数的隐式参数。因此,在成员函数内部,可以使用 this 来引用调用该函数的对象。
📋 目录
  1. this 指针
  2. this 指针示例
  3. 使用 this 指针返回调用对象的引用
  4. "this" 指针的特性
  5. const 成员函数与 static 成员函数中的 this 指针
  6. this 指针的常见用例
  7. this 指针的限制
A A

C++ this 指针



Previous
Quiz
Next

this 指针

C++ 中的每个对象都通过一个重要的指针称为 this 指针来访问自身的地址。this 指针是所有成员函数的隐式参数。因此,在成员函数内部,可以使用 this 来引用调用该函数的对象。

友元函数没有 this 指针,因为友元不是 class 的成员。只有成员函数才有 this 指针。

this 指针示例

让我们通过以下示例来理解 this 指针的概念 −

#include <iostream>
 
using namespace std;

class Box {
   public:
      // 构造函数定义
      Box(double l = 2.0, double b = 2.0, double h = 2.0) {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
      }
      double Volume() {
         return length * breadth * height;
      }
      int compare(Box box) {
         return this->Volume() > box.Volume();
      }
      
   private:
      double length;     // 盒子的长度
      double breadth;    // 盒子的宽度
      double height;     // 盒子的高度
};

int main(void) {
   Box Box1(3.3, 1.2, 1.5);    // 声明 box1
   Box Box2(8.5, 6.0, 2.0);    // 声明 box2

   if(Box1.compare(Box2)) {
      cout << "Box2 is smaller than Box1" <<endl;
   } else {
      cout << "Box2 is equal to or larger than Box1" <<endl;
   }
   
   return 0;
}

当上述代码编译并执行时,会产生以下结果 −

Constructor called.
Constructor called.
Box2 is equal to or larger than Box1

使用 this 指针返回调用对象的引用

要实现链式函数调用,需要调用对象的引用。你可以使用 "this" 指针来返回调用对象的引用。

语法

语法如下:

Test& Test::func ()
{
   return *this;
}

示例

以下示例演示了如何返回调用对象的引用:

#include <iostream>
using namespace std;

class Coordinates {
 private:
  int latitude;
  int longitude;

 public:
  Coordinates(int lat = 0, int lon = 0) {
    this->latitude = lat;
    this->longitude = lon;
  }

  Coordinates& setLatitude(int lat) {
    latitude = lat;
    return *this;
  }

  Coordinates& setLongitude(int lon) {
    longitude = lon;
    return *this;
  }

  void display() const {
    cout << "Latitude = " << latitude << ", Longitude = " << longitude << endl;
  }
};

int main() {
  Coordinates location(15, 30);

  // 链式函数调用修改同一个对象
  location.setLatitude(40).setLongitude(70);

  location.display();
  return 0;
}

当上述代码编译并执行时,会产生以下结果 −

Latitude = 40, Longitude = 70

"this" 指针的特性

  • "this" 指针指向当前对象实例,它允许成员函数访问对象属性和方法。
  • "this" 指针会隐式传递给所有非静态成员函数,你无需在代码中显式编写 this。
  • this 指向当前对象的内存位置。
  • 如果参数与成员变量名称冲突,可以使用 this 来区分成员变量与局部参数。
  • "this" 指针是常量 (const),即不可修改。
  • 由于 this 是一个指针,可以解引用它来访问当前对象。

const 成员函数与 static 成员函数中的 this 指针

在 const 成员函数中,this 指针是一个指向常量对象的指针(const MyClass*),在函数内部不能修改对象的成员,从而确保调用 const 函数时对象保持不变。

而 static 成员函数没有 this 指针,因为它们不与类的任何特定实例关联,它们属于类本身,只能访问 static 成员或方法,因为它们不操作对象特定的数据。

示例

以下示例展示了 const 成员函数与 static 成员函数中 this 指针的区别。

#include <iostream>

class MyClass {
   public:
      MyClass(int val) : data(val) {}

      // Const 成员函数(有 'this' 指针,但它是 const 指针)
      void printData() const {
         std::cout << "Data: " << data << std::endl;  // 'this' 指向一个 const 对象
         // 取消注释下一行会导致错误,因为 'this' 是 const
         // data = 10;  // 错误:在 const 成员函数中不能修改 'data'
      }

      // Static 成员函数(没有 'this' 指针,操作类级数据)
      static void showMessage() {
         std::cout << "This is a static function!" << std::endl;
         // 取消注释下一行会导致错误,因为 static 函数无法访问实例成员
         // std::cout << "Data: " << data << std::endl;  // 错误:'data' 不可访问
      }

   private:
      int data;
};

int main() {
   MyClass obj(5);

   // 调用 const 成员函数(可以作为 const 访问 'this')
   obj.printData();  

   // 调用 static 成员函数(没有可用的 'this' 指针)
   MyClass::showMessage();  

   return 0;
}

输出

Data: 5
This is a static function!

this 指针的常见用例

在 C++ 中,this 指针是一个特殊的指针,在 non-static 成员函数中它指向当前类的当前实例。

下面我们将看到它的常见用例。

  • this 指针有助于在 assignment operator 中防止自赋值,确保对象不会将自身赋值给自身。
  • this 指针通过返回当前对象(通常通过 *this)使 method chaining 成为可能,允许在一行代码中对同一对象调用多个方法。
  • 它允许在成员函数中直接访问对象的成员。
  • 在 copy constructor 和 assignment operator 中它非常重要,因为它有助于在赋值时返回当前对象。
  • 它在 Polymorphism、多继承类和实现 Fluent Interfaces 中也很有用,可以实现流畅的方法链式调用。

this 指针的限制

"this" 指针是 C++ 中的一个强大特性,但它有一些限制和潜在陷阱,开发者应该注意这些问题以防止错误或意外行为。

  • this 指针在 static 成员函数中不可用,因为 static 函数与类本身绑定,而不是与任何特定对象绑定。
  • 它可以帮助区分名称重叠的成员变量和局部变量。但是,如果局部变量遮蔽了成员变量,仍然可能导致混淆或歧义。
  • this 指针始终指向当前对象,但如果在对象销毁后或销毁过程中使用它,可能会导致 undefined behavior。
  • 在处理多继承时,如果不同的基类有相同名称的成员,可能会发生冲突。这会使 this 指针指向哪个成员变得不清楚,导致歧义。
  • 从临时对象返回 *this 可能有风险,因为它可能会留下悬垂引用,导致意外或 undefined behavior。