std::unique_ptr::operator=
Da cppreference.com
< cpp | memory | unique ptr
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
unique_ptr& operator=( unique_ptr&& r ); |
(1) | (desde C++11) |
template< class U, class E > unique_ptr& operator=( unique_ptr<U,E>&& r ); |
(1) | (desde C++11) |
unique_ptr& operator=( nullptr_t ); |
(2) | (desde C++11) |
Transferir a propriedade do objeto apontado por
2) r
para *this como se chamando reset(r.release()) seguido por uma atribuição de std::forward<E>(r.get_deleter()). Original:
Transfers ownership of the object pointed to by
r
to *this as if by calling reset(r.release()) followed by an assignment from std::forward<E>(r.get_deleter()). 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.
Efetivamente, o mesmo que chamar reset().
Original:
Effectively the same as calling reset().
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-se que a atribuição
unique_ptr
operador só aceita XValues, que são tipicamente geradas por std::move. (A classe unique_ptr
explicitamente exclui seu construtor de cópia e operador de atribuição lvalue lvalue.)Original:
Note that
unique_ptr
's assignment operator only accepts XValues, which are typically generated by std::move. (The unique_ptr
class explicitly deletes its lvalue copy constructor and lvalue assignment operator.)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.
Índice |
[editar] Parâmetros
r | - | ponteiro inteligente de que a propriedade será transferida
Original: smart pointer from which ownership will be transfered The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[editar] Valor de retorno
*this
[editar] Exceções
[editar] Exemplo
#include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo\n"; } ~Foo() { std::cout << "~Foo\n"; } }; int main() { std::unique_ptr<Foo> p1; { std::cout << "Creating new Foo...\n"; std::unique_ptr<Foo> p2(new Foo); p1 = std::move(p2); std::cout << "About to leave inner block...\n"; // Foo instance will continue to live, // despite p2 going out of scope } std::cout << "About to leave program...\n"; }
Saída:
Creating new Foo... Foo About to leave inner block... About to leave program... ~Foo