Этот вопрос является дополнительным вопросом к этому вопросу здесь: -ptr-or-a-shared-ptr">исходный вопрос
У меня есть класс, который наследуется от 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.
}