C ++“ this”指针
在C++编程语言中,“this”是关键字,“this”指针的值是在其中调用成员函数的对象的地址。
'this'指针的类型为“Class指针”(或“Objet指针”)类型-如果存在一个名为“Example”的类,则'this'指针的类型为“Example*”。
其类型可能取决于在其中使用“this”指针的成员函数声明。如果将成员函数声明为const,volatile或constvolatile-“this”指针的类型分别为constExample*,volatileExample*或constvolatileExample*。
当局部变量和类数据成员相同时(在定义成员函数时)使用“this”
#include <iostream> using namespace std; class Number { private: int a; public: void get_a(int a) {this->a=a;} void put_a() {cout<<"a= "<<a<<endl;} }; int main(){ Number N; N.get_a(36); N.put_a(); return 0; }
a= 36
在这里,a是成员函数get_a()的局部变量,也是类成员数据类型。声明this->a=a;复制类数据成员a中的(局部变量)的值。