这个问题是这个问题的后续问题:原始问题
我有一个继承自的类,std::enable_shared_from_this
该类包含一个std::shared_ptr<Self>
在我知道该类的详细信息完整且成功之后,在此类的任何构造函数中,如何将存储的内容分配std::shared_ptr<Self>
为shared this
?
例子:
class Self : public std::enable_shared_from_this<Self> {
private:
std::shared_ptr<Self> me_; // Or
std::unique_ptr>Self> me_;
public:
Self ( /*some parameters*/ );
};
Self::Self( /* some parameters */ ) {
// Check parameters for creation
// Some work or initialization being done
// If all is successful and construction of this class is about
// to leave scope, then set the smart pointer to the this*
// How to do ...
me_ = std::enable_shared_from_this<Self>::shared_from_this();
// Properly if this is even possible at all.
}