1、this指针指向本类对象的起始地址;
2、当类中数据成员与成员函数中的形参名相同时,用this指针加以区分;
#include<iostream>
using namespace std;
class Test{
private:
int x;
public:
Test(int = 0);
void print();
};
Test::Test(int a){
x=a;}
void Test::print(){
int x = 20;
cout<<"x="<<x<<endl;
cout<<"this->x="<<this->x<<endl;
cout<<"(*this).x="<<(*this).x<<endl;
}
int main(){
Test MyTest(12);
MyTest.print();
return 0;
}
运行结果:
x=20
this->x=12
(*this).x=12