这里算是为我的智能指针学习补上最后一块知识。
1. shared_from_this只能被shared_ptr管理对象调用
在cppreference里有这么一句话
![注意只容许在先前共享的对象,即 std::shared_ptr 所管理的对象上调用 shared_from_this 。(特别是不能在构造 *this 期间 shared_from_this 。)](https://i-blog.csdnimg.cn/blog_migrate/39ce10710d880e693df5219af847bcd7.png)
这里提到了这么一句话,只允许在shared_ptr所管理的对象上调用shared_from_this
。剩下的俩种管理,一种为栈对象,一种为被new分配出来的对象所管理。
class Foo
: public enable_shared_from_this<Foo>
{
public:
shared_ptr<Foo> returnSp()
{
return shared_from_this();
}
};
int