この質問はここにこの質問のフォローアップの質問ですenable_shared_from_this:original questionセルフ
私は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.
}
@ Jason R oh okay。だから私は、それらのインスタンスを含む "マネージャー型のクラス"を持っている必要があるか、または私はそれを構造体や初期化関数などのコンストラクターの直後に呼び出される関数で設定する必要があります。 –
自己へのshared_ptrは矛盾です。一時的にオブジェクトを生かしておくだけで便利です(例えば、コールバックを待つ間)。 –