this指针用法
#include <iostream>
class Person
{
public:
//解决名称冲突
void age_set(int age)
{
//this指针指向 被调用的成员函数 所属的对象
this->age = age;
}
Person &age_add(int age)
{
this->age += age;
//返回对象本身
return *this;
}
int age_get()
{
return this->age;
}
private:
int age;
};
int main()
{
Person p;
p.age_set(10);
p.age_add(1).age_add(2).age_add(3);
std::cout << "age " << p.age_get() << std::endl;
return 0;
}
$ ./a.out
age 16