Espaços nominais
Variantes
Acções

std::unique_ptr

Da cppreference.com
< cpp‎ | memory


 
 
Biblioteca de utilitários
Digite apoio (basic types, RTTI, type traits)
Gerenciamento de memória dinâmica
De tratamento de erros
Utilidades do programa
Variadic funções
Data e hora
Objetos de função
initializer_list(C++11)
bitset
hash(C++11)
Os operadores relacionais
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 e 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)
Troque, avançar e avançar
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)
 
Gerenciamento de memória dinâmica
Gerenciamento de memória de baixo nível
Alocadores
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)
Uninitialized armazenamento
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
Ponteiros 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(obsoleta)
owner_less(C++11)
enable_shared_from_this(C++11)
bad_weak_ptr(C++11)
default_delete(C++11)
Apoio a coleta de lixo
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)
Diversos
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
Funções de membro
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
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[]
Não-membros funções
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) (a partir do C++ 11)
template <

    class T,
    class Deleter

> class unique_ptr<T[],Deleter>;
(2) (a partir do C++ 11)
std::unique_ptr é um ponteiro 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.
  • detém a propriedade exclusiva de um objeto através de um ponteiro, e
    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.
  • destrói a pontas-de se opor quando o unique_ptr sai do escopo.
    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 não é copiável ou cópia transferível, duas instâncias de unique_ptr não pode gerenciar o mesmo objeto. A unique_ptr não-const pode transferir a propriedade do objeto gerenciado para outro unique_ptr. A const std::unique_ptr não pode ser transferida, o que limita o tempo de vida do objecto gerido com o âmbito em que o ponteiro foi criado. Quando o unique_ptr é destruído, ele descarta o objeto através 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.
Existem duas versões do 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)
gerencia o tempo de vida de um único objeto, por exemplo, alocados com novo
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)
controla o tempo de vida de uma matriz com um comprimento de tempo de execução, por exemplo, alocado com 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.
Os 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.
  • proporcionando segurança exceção a classes e funções que manipulam objetos com vida dinâmica, garantindo a eliminação de ambos saída normal e saída através de exceção.
    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.
  • passando a propriedade de objetos de propriedade exclusiva com vida dinâmica em funções
    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.
  • a aquisição da propriedade de objetos de propriedade exclusiva com vida dinâmica de funções
    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 o tipo de elemento em movimento de reconhecimento de recipientes, como std::vector, que possuem ponteiros para objetos dinamicamente alocados, por exemplo, se o comportamento polimórfico é desejado
    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.

Índice

[editar] Tipos de membro

Tipo de membro
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 se esse tipo existe, de outra forma * 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, o tipo do objeto gerenciado 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, o objeto de função ou lvalue referência à função ou objeto de função, a ser chamado a partir do destruidor
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] Funções de membro

constrói um novo 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.

(função pública membro) [edit]
destrói o objeto gerenciado se for 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.

(função pública membro) [edit]
atribui o 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.

(função pública membro) [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.
retorna um ponteiro para o objeto gerenciado e libera a propriedade
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.

(função pública membro) [edit]
substitui o objeto gerenciado
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.

(função pública membro) [edit]
troca os objetos gerenciados
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.

(função pública membro) [edit]
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.
retorna um ponteiro para o objeto gerenciado
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.

(função pública membro) [edit]
retorna o deleter que é usado para a destruição do objecto gerido
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.

(função pública membro) [edit]
verifica se existe é associado objeto gerenciado
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.

(função pública membro) [edit]
Único objeto de versão, 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.
dereferences ponteiro para o objeto gerenciado
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.

(função pública membro) [edit]
Versão matriz, 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.
fornece acesso indexado à matriz gerenciada
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.

(função pública membro) [edit]

[editar] Não-membros funções

compara a outra ou com 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.

(modelo de função) [edit]
o algoritmo especializado 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.

(modelo de função) [edit]

[editar] Classes auxiliares

apoio de 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.

(especialização modelo. classe) [edit]

[editar] Exemplo

#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