std::enable_shared_from_this::shared_from_this
De cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
<metanoindex/>
<tbody> </tbody> shared_ptr<T> shared_from_this(); |
(1) | |
shared_ptr<T const> shared_from_this() const; |
(2) | |
Retourne une
std::shared_ptr<T> qui partage la propriété de *this avec tous std::shared_ptr<T> existant qui se réfèrent à *this .Original:
Returns a
std::shared_ptr<T> that shares ownership of *this with all existing std::shared_ptr<T> that refer to *this.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Notes
Avant d'appeler
shared_from_this, il devrait y avoir au moins un std::shared_ptr<T> p qui possède *this .Original:
Before calling
shared_from_this, there should be at least one std::shared_ptr<T> p that owns *this.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Retourne la valeur
std::shared_ptr<T> qui partage la propriété avec des *this préexistantes std::shared_ptr<T>sOriginal:
std::shared_ptr<T> that shares ownership of *this with pre-existing std::shared_ptr<T>sThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Exemple
#include <iostream>
#include <memory>
struct Foo : public std::enable_shared_from_this<Foo> {
Foo() { std::cout << "Foo::Foo\n"; }
~Foo() { std::cout << "Foo::~Foo\n"; }
std::shared_ptr<Foo> getFoo() { return shared_from_this(); }
};
int main() {
Foo *f = new Foo;
std::shared_ptr<Foo> pf1;
{
std::shared_ptr<Foo> pf2(f);
pf1 = pf2->getFoo(); // shares ownership of object with pf2
}
std::cout << "pf2 is gone\n";
}
Résultat :
Foo::Foo
pf2 is gone
Foo::~Foo
Voir aussi
(C++11) |
pointeur intelligent avec sémantique de propriétaires partagés (classe générique) |