如何理解C++中的this指针

this指针
1、this是指向当前对象的指针,它只能在一个类的成员函数中调用,它表示当前对象的地址
2、this在成员函数的开始前构造的,在成员结束后清除,他的生命周期与函数的参数是一样的。当调用一个类的成员函数时,编译器会将类的指针作为函数的this参数传进去
3、this指针可能存放在栈区,也可能在寄存器中、甚至是全局变量
4、通过this指针访问成员变量时,要有this -> 成员变量

class Person{
public:
	int m_age;
	int m_height;
	
	void Display(){
		cout << "m_age=" << this->m_age << endl;
		cout << "m_height=" << this->m_height << endl;
	}
};

猜你喜欢

转载自blog.csdn.net/qq_47329614/article/details/106898913