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

Konstruiert ein neues enable_shared_from_this Objekt. weak_this wird wertinitialisiert .

Inhaltsverzeichnis

Parameter

other - ein enable_shared_from_this zu kopieren

Hinweise

Es gibt keinen Move-Konstruktor: Das Verschieben von einem abgeleiteten Objekt von enable_shared_from_this überträgt nicht seine gemeinsame Identität.

Beispiel

#include <memory>
struct Foo : public std::enable_shared_from_this<Foo>
{
    Foo() {} // ruft implizit den enable_shared_from_this-Konstruktor auf
    std::shared_ptr<Foo> getFoo() { return shared_from_this(); }
};
int main()
{
    std::shared_ptr<Foo> pf1(new Foo);
    auto pf2 = pf1->getFoo(); // teilt sich den Besitz des Objekts mit pf1
}

Siehe auch

(C++11)
Intelligenter Zeiger mit Shared-Object-Ownership-Semantik
(Klassentemplate)