this指向当前对象。在c++中,每一个对象都能通过this 指针来访问自己的地址。this指针是所有成员函数的隐含参数。因此在成员函数的内部,他可以用来指向调用该函数的对象。主要作用区分同名成员变量和参数。
友元函数没有this指针,因为他不是类内部函数。
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class people {
public:
int n;
people(int n) {
this->n = n;
cout << this->n << endl;
}
};
int main() {
people p1(2);
people p2(6);
return 0;
}