具体代码如下:
#include <iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
//this指针指向 被调用的成员函数 所属的对象
this->age = age;
}
int age;
Person& PersonAddage(Person &p)
{
this->age += p.age;
//this指向p2的指针,而*this指向的就是这个p2对象的本体
return *this;
}
};
//1.解决名称冲突
void test01()
{
Person p1(18);
cout << "p1的年龄为:" << p1.age << endl;
}
//2.返回对象本身用*this
void test02()
{
Person p1(10);
Person p2(10);
//链式编程思想
p2.PersonAddage(p1).PersonAddage(p1).PersonAddage(p1);
cout << "p2的年龄为:" << p2.age << endl;
}
int main()
{
system("color f5");
//test01();
test02();
system("pause");
return 0;
}