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) (depuis C++11)
enable_shared_from_this ( const enable_shared_from_this & other ) noexcept ;
(2) (depuis C++11)

Construit un nouvel objet enable_shared_from_this . weak_this est initialisé par valeur .

Table des matières

Paramètres

autre - un enable_shared_from_this à copier

Notes

Il n'y a pas de constructeur de déplacement : déplacer un objet dérivé de enable_shared_from_this ne transfère pas son identité partagée.

Exemple

#include <memory>
struct Foo : public std::enable_shared_from_this<Foo>
{
    Foo() {} // appelle implicitement le constructeur de 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(); // partage la propriété de l'objet avec pf1
}

Voir aussi

(C++11)
pointeur intelligent avec sémantique de propriété partagée d'objet
(modèle de classe)