Espacios de nombres
Variantes
Acciones

std::unique_ptr

De cppreference.com
< cpp‎ | memory


 
 
Utilidades de la biblioteca
Escriba apoyo (basic types, RTTI, type traits)
Gestión de memoria dinámica
Gestión de errores
Programa de servicios públicos
Funciones variadic
Fecha y hora
Objetos Function
initializer_list(C++11)
bitset
hash(C++11)
Operadores relacionales
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
Pares y tuplas
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
Cambiar, hacia adelante y mover
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
Gestión de memoria dinámica
Bajo la gestión de memoria de nivel
Los asignadores
Original:
Allocators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
allocator
allocator_traits(C++11)
allocator_arg_t(C++11)
allocator_arg(C++11)
uses_allocator(C++11)
scoped_allocator_adaptor(C++11)
Inicializar el almacenamiento
Original:
Uninitialized storage
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
uninitialized_copy
uninitialized_copy_n(C++11)
uninitialized_fill
uninitialized_fill_n
raw_storage_iterator
get_temporary_buffer
return_temporary_buffer
Punteros inteligentes
Original:
Smart pointers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr(C++11)
shared_ptr(C++11)
weak_ptr(C++11)
auto_ptr(obsoleto)
owner_less(C++11)
enable_shared_from_this(C++11)
bad_weak_ptr(C++11)
default_delete(C++11)
Garbage soporte colección
Original:
Garbage collection support
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
declare_reachable(C++11)
undeclare_reachable(C++11)
declare_no_pointers(C++11)
undeclare_no_pointers(C++11)
pointer_safety(C++11)
get_pointer_safety(C++11)
Varios
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pointer_traits(C++11)
addressof(C++11)
align(C++11)
C Library
Original:
C Library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
std :: unique_ptr
Las funciones miembro
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr::unique_ptr
unique_ptr::~unique_ptr
unique_ptr::operator=
Modificadores
Original:
Modifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr::release
unique_ptr::reset
unique_ptr::swap
Los observadores
Original:
Observers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr::get
unique_ptr::get_deleter
unique_ptr::operator bool
unique_ptr::operator*
unique_ptr::operator->
unique_ptr::operator[]
Terceros funciones
Original:
Non-member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
operator==
operator!=
operator<
operator>
operator<=
operator>
operator>=
std::swap
std::hash
 
Defined in header <memory>
template<

    class T,
    class Deleter = std::default_delete<T>

> class unique_ptr;
(1) (desde C++11)
template <

    class T,
    class Deleter

> class unique_ptr<T[],Deleter>;
(2) (desde C++11)
std::unique_ptr es un puntero inteligente que:
Original:
std::unique_ptr is a smart pointer that:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • mantiene la propiedad exclusiva de un objeto a través de un puntero, y
    Original:
    retains sole ownership of an object through a pointer, and
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • destruye al cual apunta objeto cuando el unique_ptr sale del ámbito .
    Original:
    destroys the pointed-to object when the unique_ptr goes out of scope.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
unique_ptr no es copiable o copia cedible, dos instancias de unique_ptr no puede administrar el mismo objeto. A unique_ptr no constante puede transferir la propiedad del objeto administrado a otro unique_ptr. Un const std::unique_ptr no pueden ser transferidos, lo que limita el tiempo de vida del objeto gestionado para el ámbito en el que se creó el puntero. Cuando el unique_ptr se destruye, se elimina el objeto a través de Deleter .
Original:
unique_ptr is not copyable or copy-assignable, two instances of unique_ptr cannot manage the same object. A non-const unique_ptr can transfer the ownership of the managed object to another unique_ptr. A const std::unique_ptr cannot be transferred, limiting the lifetime of the managed object to the scope in which the pointer was created. When the unique_ptr is destroyed, it disposes of the object through Deleter.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Hay dos versiones de std::unique_ptr:
Original:
There are two versions of std::unique_ptr:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
administra la duración de un solo objeto, por ejemplo, asignado con uno nuevo
Original:
manages the lifetime of a single object, e.g. allocated with new
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
administra la duración de una matriz con una longitud de tiempo de ejecución, por ejemplo, asignado con new []
Original:
manages the lifetime of an array with a runtime length, e.g. allocated with new[]
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Los usos típicos de std::unique_ptr incluir
Original:
Typical uses of std::unique_ptr include
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • proporcionar seguridad a excepción clases y funciones que manejan los objetos con vida dinámico, garantizando la eliminación tanto de salida normal y salir por excepción .
    Original:
    providing exception safety to classes and functions that handle objects with dynamic lifetime, by guaranteeing deletion on both normal exit and exit through exception.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • pasando la propiedad de los objetos de propiedad exclusiva con vida dinámica en funciones
    Original:
    passing ownership of uniquely-owned objects with dynamic lifetime into functions
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • adquirir la propiedad de los objetos de propiedad exclusiva con vida dinámica de las funciones
    Original:
    acquiring ownership of uniquely-owned objects with dynamic lifetime from functions
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • como el tipo de elemento en movimiento conscientes recipientes, tales como std::vector, que contienen punteros a los objetos asignados de forma dinámica, por ejemplo, si el comportamiento polimórfico se desea
    Original:
    as the element type in move-aware containers, such as std::vector, which hold pointers to dynamically-allocated objects, e.g. if polymorphic behavior is desired
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Contenido

[editar] Tipos de miembros

Miembro de tipo
Original:
Member type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Definition
pointer
std::remove_reference<D>::type::pointer si ese tipo existe, de lo contrario * T
Original:
std::remove_reference<D>::type::pointer if that type exists, otherwise T*
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
element_type
T, el tipo de objeto gestionado por este unique_ptr
Original:
T, the type of the object managed by this unique_ptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
deleter_type
Deleter, el objeto de la función o referencia lvalue para funcionar o para objeto de función, que se llamará desde el destructor
Original:
Deleter, the function object or lvalue reference to function or to function object, to be called from the destructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Las funciones miembro

construye una nueva unique_ptr
Original:
constructs a new unique_ptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(miembro público función) [edit]
destructs el objeto administrado tal si está presente
Original:
destructs the managed object if such is present
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(miembro público función) [edit]
asigna el unique_ptr
Original:
assigns the unique_ptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(miembro público función) [edit]
Modificadores
Original:
Modifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
devuelve un puntero al objeto administrado y libera la propiedad
Original:
returns a pointer to the managed object and releases the ownership
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(miembro público función) [edit]
reemplaza el objeto administrado
Original:
replaces the managed object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(miembro público función) [edit]
intercambia los objetos gestionados
Original:
swaps the managed objects
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(miembro público función) [edit]
Los observadores
Original:
Observers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
devuelve un puntero al objeto gestionado
Original:
returns a pointer to the managed object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(miembro público función) [edit]
devuelve el Deleter que se utiliza para la destrucción del objeto administrado
Original:
returns the deleter that is used for destruction of the managed object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(miembro público función) [edit]
comprueba si se asocia objeto administrado
Original:
checks if there is associated managed object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(miembro público función) [edit]
Un solo objeto de versión, unique_ptr <T>
Original:
Single-object version, unique_ptr<T>
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
desreferencia puntero al objeto gestionado
Original:
dereferences pointer to the managed object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(miembro público función) [edit]
Versión Array, unique_ptr <T[]>
Original:
Array version, unique_ptr<T[]>
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
proporciona acceso indexado a la matriz administrada
Original:
provides indexed access to the managed array
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(miembro público función) [edit]

[editar] Terceros funciones

se compara con otro o con unique_ptr nullptr
Original:
compares to another unique_ptr or with nullptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(función de plantilla) [edit]
el algoritmo se especializa std::swap
Original:
specializes the std::swap algorithm
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(función de plantilla) [edit]

[editar] Clases de ayuda

apoyo hash para std::unique_ptr
Original:
hash support for std::unique_ptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(clase de especialización de plantilla) [edit]

[editar] Ejemplo

#include <iostream>
#include <memory>
 
struct Foo {
    Foo() { std::cout << "Foo::Foo\n"; }
    ~Foo() { std::cout << "Foo::~Foo\n"; }
    void bar() { std::cout << "Foo::bar\n"; }
};
 
void f(const Foo &foo)
{
    std::cout << "f(const Foo&)\n";
}
 
int main()
{
    std::unique_ptr<Foo> p1(new Foo);  // p1 owns Foo
    if (p1) p1->bar();
 
    {
        std::unique_ptr<Foo> p2(std::move(p1));  // now p2 owns Foo
        f(*p2);
 
        p1 = std::move(p2);  // ownership returns to p1
        std::cout << "destroying p2...\n";
    }
 
    if (p1) p1->bar();
 
    // Foo instance is destroyed when p1 goes out of scope
}

Output:

Foo::Foo
Foo::bar
f(const Foo&)
destroying p2...
Foo::bar
Foo::~Foo