1.可以使用 *this返回当前对象
2.常函数中不可修改成员变量的值 (mutable)关键字处理修改
常对象只能调用常函数
#include <iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
this->age = age;
}
Person& personAdd(int age)
{
this->age += age;
return *this;
}
//常函数
void showPerson() const
{
//this->age = 10;
this->name = 60;
}
mutable int name;
int age;
};
int main()
{
Person p(10);
p.personAdd(10).personAdd(10).personAdd(10);
cout << "年龄:" << p.age << endl;
const Person p1(20);
//p1.personAdd(10);
p1.showPerson();
return 0;
}
int main()
{
Person p(10);
p.personAdd(10).personAdd(10).personAdd(10);
cout << "年龄:" << p.age << endl;
const Person p1(20);
//p1.personAdd(10);
p1.showPerson();
return 0;
}