【C++ | this指针】一文了解C++的this指针

😁博客主页😁:🚀https://blog.csdn.net/wkd_007🚀
🤑博客内容🤑:🍭嵌入式开发、Linux、C语言、C++、数据结构、音视频🍭
⏰发布时间⏰:

本文未经允许,不得转发!!!


在这里插入图片描述

🎄一、为什么需要 this 指针

用下面这个例子说明一下,为什么需要 this 指针。

我们写一个CDate类,在类里面定义一个成员函数BigYear(),负责比较两个CDate对象,并返回年份较大的对象。

class CDate
{
public:
	const CDate& BigYear(const CDate& date)
	{
		if(date.m_year > m_year)
			return date;	// 参数对象
		else
			return ????;	// 调用对象
	}

private:
	int m_year;
	int m_mon;
	int m_day;
};

上面代码中,BigYear成员函数会出现一个问题,假设我们定义了两个CDate对象date_1date_2,并使用语句date_1.BigYear(date_2);来调用BigYear函数时,我们可以将参数对象(date_2的引用)作为返回值,但调用对象date_1却似乎没有办法返回,因为没有它的引用。

要解决这个问题,就需要使用C++的 this 指针。


在这里插入图片描述

🎄二、什么是 this 指针

this 指针指向用来调用成员函数的对象 ( this 被作为隐藏参数传递给类的成员函数)。

一般来说, 所有的类的成员函数( 包括构造函数和析构函数 ) 都有一个 this 指针。this 指针指向调用对象。如果成员函数需要引用整个调用对象,则可以使用表达式*this

this 指针的值不能被修改,this 指针指向的内容可以被修改,所以,BigYear 的函数头可能是下面代码的样子,只是这个this参数是我们看不见的,但可以直接使用:

const CDate& BigYear(CDate* const this, const CDate& date);

也就是说,在类的所有成员函数,都可以直接使用 this 这个名称,它指向调用该成员函数的对象,下面就使用代码来证明这一点:
1、我们使用this指针完善了 BigYear 成员函数;
2、新增printThisObject成员函数,把调用对象指针作为参数,与tish指针的值一起打印对比,会发现是一样的,说this指针就是指向当前调用对象。
3、返回引用的函数是左值的,意味着这些函数返回的是对象本身而非对象的副本。

// g++ 10_this_Date.cpp
#include <iostream>

using namespace std;

class CDate
{
public:
	CDate(int year, int mon, int day);	// 构造函数声明
	~CDate();							// 析构函数声明

	void show()
	{
		cout << "Date: " << m_year << "." << m_mon << "." << m_day << endl;
	}
	
	const CDate& BigYear(const CDate& date)
	{
		if(date.m_year > m_year)
			return date;
		else
			return (*this);
	}
	
	void printThisObject(const CDate* this_object)
	{
		cout << "this_object=" << this_object << ", this=" << this <<endl;
	}

private:
	int m_year;
	int m_mon;
	int m_day;
};

// 构造函数定义
CDate::CDate(int year, int mon, int day)
{
	m_year = year;
	m_mon = mon;
	m_day = day;
	cout << "Calling Constructor" << ", this=" << this <<endl;
}

// 析构函数定义
CDate::~CDate()
{
	cout << "Calling Destructor" << ", this=" << this <<endl;
}

int main()
{
	CDate date_1(2024,06,05);
	date_1.printThisObject(&date_1);
	
	cout << endl;
	
	CDate date_2(2025,06,05);
	date_2.printThisObject(&date_2);
	
	cout << endl;
	
	date_1.BigYear(date_2);
	
	return 0;
}

运行结果如下:
在这里插入图片描述


在这里插入图片描述

🎄三、使用 this 指针

在C++编程中,this指针在下面几个场景特别有用:

  1. 区分函数参数和数据成员:
    我们之前说明,成员函数参数最好不要跟数据成员名称一致,因为这样无法区分,而this指针可以帮助区分,看下面例子:

    class CDate
    {
    public:
    	CDate(int year, int mon, int day)
    	{
    		// year = year; // 会报错,无法区分成员和参数
    		this->year = year;
    		this->mon = mon;
    		this->day = day;
    	}
    
    private:
    	int year;
    	int mon;
    	int day;
    };
    
  2. 作为函数返回值

    const CDate& BigYear(const CDate& date)
    {
       ...
       return *this;
    }
    CDate* const BigYear()
    {
       ...
       return this;
    }
    
  3. 作为函数实参,这个就不举例的,就是将this*this 传给某个函数使用。

在这里插入图片描述

🎄四、总结

👉本文介绍了C++的this指针,为什么需要this指针,什么是this指针,怎么使用this指针。

this指针是所有类成员函数隐藏的一个函数参数:类名 * const this,不能显示地看到,但可以直接使用。
this指针指向当前成员函数的调用对象。

在这里插入图片描述
如果文章有帮助的话,点赞👍、收藏⭐,支持一波,谢谢 😁😁😁

  • 53
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 33
    评论
评论 33
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wkd_007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值