0
点赞
收藏
分享

微信扫一扫

git stage 和 git unstage

七千22 2024-08-13 阅读 56

学习内容

        类和对象_this指针的用途

        1.解决名称冲突

        2.返回对象本身用 *this

运行结果

代码

#include<iostream>
using namespace std;//cout 在这里,没有它会报错

//1.解决名称冲突

//2.返回对象本身用 *this

class Person
{
public:
	Person(int age)
	{
		//形参名称不能和成员名称一样,推荐以m_开头
		//age = age;
		m_Age = age;//推荐
		this->age = age;//1.解决名称冲突, 这样也行,这是this的一种用法。
	}

	int age;
	int m_Age;//推荐

	Person& AddAge(Person& p)
	{
		this->age += p.age;
		return *this;//2.返回对象本身用 *this
	}
};

int main()
{
	Person p1(10);

	Person p2(10);
	//链式编程
	p2.AddAge(p1).AddAge(p1).AddAge(p1);

	cout << "年龄:" << p2.age << endl;

	system("pause");
}

坚持学习,每天进步一点点。加油。
今天看一个影评《人生大事》,有空去看一下电影

举报

相关推荐

0 条评论