Namespaces
Variants

std::enable_shared_from_this<T>:: enable_shared_from_this

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
constexpr enable_shared_from_this ( ) noexcept ;
(1) (начиная с C++11)
enable_shared_from_this ( const enable_shared_from_this & other ) noexcept ;
(2) (начиная с C++11)

Создает новый объект enable_shared_from_this . weak_this подвергается инициализации значением .

Содержание

Параметры

другой - объект enable_shared_from_this для копирования

Примечания

Нет конструктора перемещения: перемещение из объекта, производного от enable_shared_from_this не передает его общую идентичность.

Пример

#include <memory>
struct Foo : public std::enable_shared_from_this<Foo>
{
    Foo() {} // неявно вызывает конструктор enable_shared_from_this
    std::shared_ptr<Foo> getFoo() { return shared_from_this(); }
};
int main()
{
    std::shared_ptr<Foo> pf1(new Foo);
    auto pf2 = pf1->getFoo(); // разделяет владение объектом с pf1
}

Смотрите также

(C++11)
умный указатель с семантикой совместного владения объектом
(шаблон класса)