std::make_shared
De cppreference.com
< cpp | memory | shared ptr
Revisión a fecha de 23:56 1 jul 2013; P12bot (Discusión | contribuciones)
![]() |
Esta página se ha traducido por ordenador/computador/computadora de la versión en inglés de la Wiki usando Google Translate.
La traducción puede contener errores y palabras aparatosas/incorrectas. Planea sobre el texto para ver la versión original. Puedes ayudar a corregir los errores y mejorar la traducción. Para instrucciones haz clic aquí. |
Definido en el archivo de encabezado <memory>
|
||
template< class T, class... Args > shared_ptr<T> make_shared( Args... args ); |
||
Construye un objeto de tipo
T
y lo envuelve en una std::shared_ptr utilizando args
como la lista de parámetros del constructor de 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.
Contenido |
Parámetros
args | - | lista de argumentos con los que va a ser una instancia de
T construidos .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. |
Valor de retorno
std::shared_ptr de una instancia de tipo
T
.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.
Excepciones
Puede lanzar std::bad_alloc o cualquier excepción lanzada por el contructor de
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.
Notas
Esta función asigna memoria para el objeto T y para el bloque de control de la shared_ptr con una asignación de memoria. En contraste, la declaración
std::shared_ptr<T> p(new T(Args...))
realiza dos asignaciones de memoria, que puede incurrir en una sobrecarga innecesaria .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.
Ejemplo
Ejecuta este código
#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; }
Salida:
11
Ver también
Construye un nuevo shared_ptr . (función miembro pública) | |
Crea un puntero compartido que gestiona un nuevo objeto asignado usando un asignador. (plantilla de función) |