std::make_shared
Da cppreference.com.
< cpp | memory | shared ptr
![]() |
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. |
Elemento definito nell'header <memory>
|
||
template< class T, class... Args > shared_ptr<T> make_shared( Args... args ); |
||
Costruisce un oggetto di tipo
T
e lo avvolge in un std::shared_ptr utilizzando args
della lista dei parametri per il costruttore di T
.Original:
Constructs an object of type
T
and wraps it in a std::shared_ptr using args
as the parameter list for the constructor of T
.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.
Indice |
[modifica] Parametri
args | - | elenco di argomenti con cui un'istanza di
T saranno costruiti .Original: list of arguments with which an instance of T will be constructed.The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[modifica] Valore di ritorno
std::shared_ptr di un'istanza di
T
tipo.Original:
std::shared_ptr of an instance of type
T
.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.
[modifica] Eccezioni
Può lanciare std::bad_alloc o qualsiasi eccezione generata dal contructor di
T
.Original:
May throw std::bad_alloc or any exception thrown by the contructor of
T
.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.
[modifica] Note
Questa funzione alloca la memoria per l'oggetto T e per il blocco di controllo del shared_ptr con una dotazione di memoria singolo. Al contrario, la dichiarazione
std::shared_ptr<T> p(new T(Args...))
esegue due allocazioni di memoria, che possono sostenere un inutile sovraccarico.Original:
This function allocates memory for the T object and for the shared_ptr's control block with a single memory allocation. In contrast, the declaration
std::shared_ptr<T> p(new T(Args...))
performs two memory allocations, which may incur unnecessary overhead.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.
[modifica] Esempio
#include <iostream> #include <memory> void foo(std::shared_ptr<int> i) { (*i)++; } int main() { auto sp = std::make_shared<int>(10); foo(sp); std::cout << *sp << std::endl; }
Output:
11
[modifica] Vedi anche
constructs new shared_ptr (metodo pubblico) | |
crea un puntatore condiviso che gestisce un nuovo oggetto allocato con un allocatore Original: creates a shared pointer that manages a new object allocated using an allocator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (funzione di modello) |