C++ this 指针
this 指针
C++ 中的每个对象都可以通过一个称为 this 的重要指针访问其自身的地址。this 指针是所有成员函数的隐式参数。因此,在成员函数内部,可以使用 this 来引用调用对象。
友元函数 没有 this 指针,因为友元函数不是类的成员。只有成员函数 才有 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; // box 盒子的长度 double breadth; // box 盒子的宽度 double height; // box 盒子的高度 }; 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 成员函数与静态成员函数中的 this 指针
在 const 成员函数中,this 指针指向常量对象 (const MyClass*),该对象的成员在函数内无法修改,因此调用 const 函数时对象保持不变。
静态成员函数没有 this 指针,因为它们不与类的任何特定实例关联,它们属于类本身,并且只能访问静态成员或方法,因为它们不操作特定于对象的数据。
示例
以下示例分别展示了在 Const 成员函数和静态成员函数中使用 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 member function (no 'this' pointer, operates on class-level data) static void showMessage() { std::cout << "This is a static function!" << std::endl; // 取消注释下一行将导致错误,因为静态函数无法访问实例成员 // std::cout << "Data: " << data << std::endl; // Error: 'data' is not accessible } private: int data; }; int main() { MyClass obj(5); // 调用 const 成员函数(可以将 this 作为 const 访问) obj.printData(); // 调用静态成员函数(没有可用的 this 指针) MyClass::showMessage(); return 0; }
Output
Data: 5 This is a static function!
this 指针的常见用例
在 C++ 中,this 指针是一个特殊指针,它在非静态成员函数中指向类的当前实例。
下面我们将介绍它的常见用例。
- this 指针有助于防止赋值运算符中的自赋值,确保对象不会将自身赋值给自身。
- this 指针通过返回当前对象(通常通过 *this )来实现方法链,从而允许在一行代码中调用同一对象上的多个方法。
- 它允许在成员函数中直接访问对象的成员。
- 它在复制构造函数和赋值运算符中非常重要,因为它有助于在赋值期间返回当前对象。
- 它在多态性、继承类和实现中也很有用。流畅接口,允许顺畅的方法链。
this 指针的局限性
"this"指针是 C++ 中的一个强大功能,但它也存在一些局限性和潜在的陷阱,开发者应该牢记这些局限性和陷阱,以防止出现错误或意外行为。
- this 指针在静态成员函数中不可用,因为静态函数与类本身绑定,而不是与任何特定对象绑定。
- 当成员变量和局部变量的名称重叠时,它可以帮助区分它们。但是,如果局部变量遮蔽了成员变量,仍然会导致混淆或歧义。
- this 指针始终指向当前对象,但在对象销毁后或销毁过程中使用它可能会导致未定义的行为。
- 处理多重继承时,如果不同的基类共享具有相同名称的成员,则可能会发生冲突。这会使 this 指针指向哪个成员变得不清楚,从而导致歧义。
- 从临时对象返回 *this 可能存在风险,因为它可能会留下悬垂引用,从而导致意外或未定义的行为。