C++学习笔记——this指针

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

猜你喜欢

转载自blog.csdn.net/wxsy024680/article/details/113703127
今日推荐