C++中的this指针相关内容总结

什么是this指针?
this指针是类“成员函数”第一个隐藏的参数,该指针时时刻刻指向调用成员函数的对象(当前对象)

先写一份关于“类”的代码,我们从中开始探究this

class Student
{
public:

	char _name[20];
	char _gender[3];
	int _age;

public:
    //下列函数代码有四个参数,另一个参数是:隐藏的this指针	
	//在写代码期间,用户不用显示给出this指针,编译器在编译代码时,会自动加上this指针的参数
	void InitStudent(char name[], char gender[], int age)
	{
		cout << this << endl;
		strcpy(this->_name, name);
		strcpy(this->_gender, gender);
		this->_age = age;
	}

	void PrintStudent()
	{
		cout << _name << "-" << _gender << "-" << _age << endl;
	}
};

int main()
{
	Student s;

	Student s1,s2,s3;
	s1.InitStudent("Peter", "男",18);
	s2.InitStudent("David", "男", 19);
	s3.InitStudent("Lily", "女", 18);

	s1.PrintStudent();
	s2.PrintStudent();
	s3.PrintStudent();

	cout << sizeof(s1) << endl;
	
	return 0; 
}

特性:
1.只能在“成员函数”中使用
自我理解:this只能在成员函数体内部进行使用
检验:

int main()
{
    cout<<this<<endl;
    return 0;
}

这段代码是运行不成功的。

给出代码中:

	void InitStudent(char name[], char gender[], int age)
	{
		cout << this << endl;
		strcpy(this->_name, name);
		strcpy(this->_gender, gender);
		this->_age = age;
	}

代码正常运行,并且输出当前对象的地址
综上两段代码我们可以得出结论

2.this指针的类型为–》类类型 举例:Student* const this
this不能被赋值
举例:

void InitStudent(char name[], char gender[], int age)
{
	this = pullptr;
	//想法:我们令this指向空,这样this就不能指向这个对象地址,将来就不能将信息设置到对象中去
	//报错:左操作数必须为左值--》不能给this赋值
	strcpy(_name, name);
	strcpy(_gender, gender);
	_age = age;
}

有了this指针,成员函数就知道实在操作那个对象
注意:对成员变量的操作都是通过this指针来进行的

3.this指针没有存储在对象中,因此不会影响对象的大小,而是在成员函数运行时,时时刻刻指向当前对象
在反汇编代码中,

void InitStudent(char name[], char gender[], int age)
{
	cout << this << endl;
	strcpy(this->_name, name);
	strcpy(this->_gender, gender);
	this->_age = age;
}

前三行使用是用通过参数压栈的方式来传参
第四行:lea exc,[s1]
lea指令:后面跟谁就是取谁的地址
第四行(lea exc,[s1])的意思为:将s1对象的地址取出来放到ecx寄存器中
exc寄存器:可以看作为——》我们在程序中定义了许多全局变量
即:在vs中,当前对象的地址是通过exc寄存器来传递的

4.this指针时“成员函数”第一个隐藏的参数,“隐藏的”–》用户在实现成员函数时,不用显示给出,该参数是编译器自动添加的,也是由编译器自动来进行传递的

	void InitStudent(char name[], char gender[], int age)
	{
		cout << this << endl;
		strcpy(this->_name, name);
		strcpy(this->_gender, gender);
		this->_age = age;
	}

即:上述代码给定的是四个参数,原本应该是:

void InitStudent(Student* this, char* name, char* gender, int age)

5.this指针主要是通过exc寄存器来传递的
注意:是主要不是都要,有一些不是。
ex:不定参数的函数
举例:
我们在给定代码的结构体中加入一个函数

void testfunc(...)
{}

我们在主函数中调用一下:

int main()
{
	Student s;

	Student s1,s2,s3;
	s1.InitStudent("Peter", "男",18);
	s2.InitStudent("David", "男", 19);
	s3.InitStudent("Lily", "女", 18);

	s1.PrintStudent();
	s2.PrintStudent();
	s3.PrintStudent();
    
    //调用一下:
	s1.testfunc(1);
	s2.testfunc(1,2);
	s3.testfunc(1,2,3);
	
	return 0; 
}

运行后,观察反汇编

s1.testfunc(1);
** push           1
** lea            eax,[s1]
** push           eax
**
**

s2.testfunc(1,2);
** push           2
** push           1
** lea            eax,[s2]
** push           eax
**
**

其中s1的第二三行为:将s1对象的地址压栈
其中s2的第三四行为:将s2对象的地址压栈
可以得出:testfunc方式的this指针是通过参数压栈的方式来进行传递的

问题:那些ecx来进行传递的,那些又不是?
将上述函数体的实现拿掉,只留下函数声明
即:

	void InitStudent(char name[], char gender[], int age);
	void testfunc(...);

只给声明不给实现,函数在运行时必定会报错
报错形式:

void _thiscall*********
void _cdecl************

_thiscall, _cdecl是什么?
答:调用约定,调用函数时所作的一些约定

c语言中

void InitStudent(char name[], char gender[], int age)
{

	strcpy(_name, name);
	strcpy(_gender, gender);
	_age = age;
}

反汇编中:

strcpy(_name, name);
** mov      eax,dword ptr [name]
** push     eax
** mov      ecx,dword ptr[this]
** push     ecx
**
**
strcpy(_gender, gender);
** mov      eax,dword ptr [gender]
** push     eax
** mov      ecx,dword ptr[this]
** push     ecx,14h
** push     ecx
**
**
_age = age;
** mov      eax,dword ptr [this]
** mov      ecx,dword ptr[age]
** mov      dword ptr [ptr+18h],ecx
**
** 

上面均为通过this给其赋值:this->
补充:
成员变量可以在类的任意位置来进行定义,而且可以在“任意成员函数”中使用
就相当于:成员变量就类似于成员函数的全局变量

常识:全局变量在使用之前必须要先定义

成员变量在后,成员变量的使用在前,编译器怎么不报错?
答:编译器处理类:
1.先识别类名
2.再来识别类中的成员变量
3.最后识别类中的成员函数,并对成员函数进行修改–》主要增加this指针

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值