C++类与对象笔记八:C++中this指针概念

2022/1/11 20:04:32

本文主要是介绍C++类与对象笔记八:C++中this指针概念,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

在上一节中,我们知道每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码区域。

问题是:多个对象同时使用同一块代码区,如何保证各自调用而不混合?

换言之:多个对象:p1,p2,p3...,同时调用某一个函数func。那么这个函数如何知道去修改哪个对象的参数呢?

C++通过提供特殊的对象指针:this指针,解决上述问题。

this指针指向被调用的成员函数所属的对象。调用者==this。

  • this指针是隐含在每一个非静态成员函数内的一种指针。
  • this指针不需要定义,直接使用即可。

this指针的用途

  • 解决名称冲突:当形参和成员变量同名时,可以用this指针来区分
  • 返回对象本身:在类的非静态成员函数中返回对象本身,可使用return *this

看一段代码:

#include<iostream>
using namespace std;

class Person
{
public:
	Person(int age)
	{
		age = age;
	}
	int age;
};


int main()
{
	Person p(18);
	cout << "年龄是:" << p.age << endl;
	system("pause");
	return 0;
}

 在Person类中,含参构造函数中三个age,三个age是同一个age,因此解决方法:

  1. 对于类内成员都添加m_区分,表明是类内成员(member)。
  2. 添加this->。this->age = age;

// this指针指向的是:被调用的成员函数所属的对象。谁调用成员函数,它就是谁。谁调用的有参构造,p,因此this就指向调用者p。

#include<iostream>
#include "07this指针的用法.h"
using namespace std;

class Person
{
public:
	Person(int age)
	{
		this->age = age;
	}

	void PersonAddAge(Person& p)
	{
		this->age += p.age;
	}
	int age;
};
void test01()
{
	Person p(18);
	cout << "年龄是:" << p.age << endl;
}

void test02()
{
	Person p1(10);
	Person p2(18);
	p2.PersonAddAge(p1);
	cout << "p2加p1之后的年龄为:" << p2.age << endl;   // 28。
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}


高潮:

更进一步,再加的情形。链式编程思想。

#include<iostream>
#include "07this指针的用法.h"
using namespace std;

class Person
{
public:
	Person(int age)
	{
		this->age = age;
	}

	Person& PersonAddAge(Person& p)   // 引用的方式返回本体。
	{
		this->age += p.age;
		return *this;
	}
	int age;
};
void test01()
{
	Person p(18);
	cout << "年龄是:" << p.age << endl;
}

void test02()
{
	Person p1(10);
	Person p2(18);
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p2);  // 链式编程思想。无限往后追加。cout中的<< 也是如此。
	cout << "p2加p1之后的年龄为:" << p2.age << endl;   //  76  
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}


一定要注意返回的是本体的引用,如果不写&,那么结果就不一样了。 

#include<iostream>
#include "07this指针的用法.h"
using namespace std;

class Person
{
public:
	Person(int age)
	{
		this->age = age;
	}

	Person PersonAddAge(Person& p)   // 引用的方式返回本体。
	{
		this->age += p.age;
		return *this;
	}
	int age;
};
void test01()
{
	Person p(18);
	cout << "年龄是:" << p.age << endl;
}

void test02()
{
	Person p1(10);
	Person p2(18);
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p2);  // 链式编程思想。无限往后追加。cout中的<< 也是如此。
	cout << "p2加p1之后的年龄为:" << p2.age << endl;   // 28
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}


返回值为28。为何?

因为返回的是值类型,前面提到返回值方式如果是一个对象,那么return就会调用拷贝构造函数,重新拷贝一份。而不是本体上继续相加。重新拷贝的那份就不再是p2了,而是p2的复制。这个复制去加后面的年龄,而不是p2的本体加,到第一个函数调用结束p2的值就不再变了。后面都是复制体干的事。与本体无关了。

用之前的返回引用类型,就没问题了。因为引用的是本体。

链式编程的思想关键是返回本体引用






这篇关于C++类与对象笔记八:C++中this指针概念的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程