std::enable_shared_from_this::shared_from_this
Da cppreference.com.
|
|
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
<metanoindex/>
<tbody> </tbody> shared_ptr<T> shared_from_this(); |
(1) | |
shared_ptr<T const> shared_from_this() const; |
(2) | |
Restituisce un
std::shared_ptr<T> che la proprietà di parti *this con tutto std::shared_ptr<T> esistenti che fanno riferimento a *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.
Note
Prima di chiamare
shared_from_this, ci dovrebbe essere almeno un std::shared_ptr<T> p che possiede *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.
Valore di ritorno
std::shared_ptr<T> che la proprietà di parti *this con pre-esistenti 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.
Esempio
#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";
}
Output:
Foo::Foo
pf2 is gone
Foo::~Foo
Vedi anche
(C++11) |
smart pointer with shared object ownership semantics (classe template) |