keep码
度过大难,将有大成。 继续努力,终成大器。
#include <iostream>
using namespace std;
#include <string>

class Person {
public:

    Person(int age)
    {
        //1、当形参和成员变量同名时,可用this指针来区分
        this->age = age;

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

void test01()
{
    Person p1(10);
    cout << "p1.age = " << p1.age << endl;

    Person p2(10);
    p2.PersonAddPerson(p1).PersonAddPerson(p1).PersonAddPerson(p1);
    cout << "p2.age = " << p2.age << endl;
}


int main()
{
    
    test01();

    system("pause");
    return 0;

}

 

posted on 2021-11-15 22:11  keep码  阅读(25)  评论(0编辑  收藏  举报