Person类
class Person
{
public:
int age = 0;
Person add(int a)
{
this->age += a;
return *this;
}
Person ()
{
cout << "construction func executing!!!" << endl;
}
Person(const Person &p)
{
cout << "copy func executing!!!" << endl;
this->age = p.age;
}
};
add函数的返回值是Person,返回的是一个对象,这是系统会调用Person类的拷贝构造函数而不是返回原来的对象指针
int main()
{
Person p;
cout << "age = " << p.age << endl;
p.add(10).add(10);
cout << "age = " << p.age << endl;
}
说明p只调用了add(10)一次,age有初始的0变成了10
原因:
p.add(10).add(10);
以上出现了匿名对象,简单了解了一下匿名对象的生命周期
可以看到p`,p``没有变量接受成为了匿名对象很快就被编译器调用析构函数释放了,被释放也是根据栈的FILO属性先释放p``再释放p`



