c++ this指针

一、this指针引入

我们知道类的大小是其普通成员变量(非静态)的大小,函数成员并不计入其中,这是因为,类中的成员函数其实就是全局函数,但缺必须用类对象来调用,这就是this指针的作用。

class Base
{
public:
	//类成员函数bfun(),编译器会给其进行唯一命名并加入参数,并加入参数
	//例如:xxxxBasexxxxbfun(Base* const, int)
	//其实也就是全局函数
	void bfun(int m);
	int b_i;
};

//全局函数 编译器也会对其进行唯一命名例如叫:xxxxxxxgfun
int gfun(int a)
{
	return a * 3;
}

Base::bfun(int)函数会被编译器编译成类似xxxxBasexxxxbfun(Base* const this, int)这样的全局函数。即增加第一个参数this指向其对象。所以对其调用:

int main()
{
	Base myb;
	myb.bfun(10);
	//就相当于调用全局函数 xxxxBasexxxxbfun(&myb, 10)
	
	return 0;
}

二、this指针调整

C是多继承Base和Base2,c的对象和Base的对象内存地址相同,但当操作Base2的成员时,需要把this指针先调整到Base2的内存地址。如下测试代码:

class Base
{
public:
	virtual  void fun(int val)
	{
		cout << "Base::fun this = " << this << endl;
	}

	virtual  ~Base() {}
};
class Base2
{
public:
	virtual void fun(int val)
	{
		cout << "Base2::fun this = " << this << endl;
	}
	virtual ~Base2() {}
};

class C :public Base, public Base2
{
public:
	void fun(int val)
	{
		cout << "C::fun this = " << this << endl;
	}
};

int main()
{
	C c1;
	cout << "c1对象地址:" << &c1 << endl;
	Base &b = c1;         //b对象的地址和c1是一样的,都指向实际内存的开头
	cout << "b对象地址:" << &b << endl;     
	Base2 &b2 = c1;       //这样得到的b2的地址却和c1不一样,所以操作b2的成员时,this指针会调整到b2  
	cout << "b2对象地址:" << &b2 << endl;
	b2.fun(10);  //
	
	return 0;
}

测试结果:

c1对象地址:0136F6E8
b对象地址:0136F6E8
b2对象地址:0136F6EC
C::fun this = 0136F6E8
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值