Moving Unique Pointers
Copying Not Allowed
A std::unique_ptr
is a smart pointer that manages a dynamically allocated object and ensures its proper deletion when it goes out of scope. It has exclusive ownership of the object it manages, meaning no two std::unique_ptr
can own the same object simultaneously.
main.cpp
123456789#include <iostream> #include <memory> int main() { std::unique_ptr<int> first_unique_pointer = std::make_unique<int>(42); // This will cause a compilation error: copying is not allowed std::unique_ptr<int> second_unique_pointer = first_unique_pointer; }
Moving a Unique Pointer
Although copying is not allowed, you can transfer ownership of the object using std::move
. This moves the resource from one std::unique_ptr
to another, leaving the original pointer empty.
main.cpp
123456789101112131415#include <iostream> #include <memory> int main() { std::unique_ptr<int> first_unique_pointer = std::make_unique<int>(42); // Transferring ownership using std::move std::unique_ptr<int> second_unique_pointer = std::move(first_unique_pointer); if (!first_unique_pointer) std::cout << "first_unique_pointer is now empty.\n"; std::cout << "second_unique_pointer owns the value: " << *second_unique_pointer << '\n'; }
When you use std::move
, ownership of the object is transferred from one unique pointer to another. After this, the original pointer no longer owns the object, and the new pointer becomes its owner.
Swipe to start coding
Complete the following code in a way that the ownership of the dynamically allocated integer (with value 42) is safely transferred from unique_pointer_a
to unique_pointer_b
.
Solution
solution.cpp
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 5.56Awesome!
Completion rate improved to 5.56
Moving Unique Pointers
Copying Not Allowed
A std::unique_ptr
is a smart pointer that manages a dynamically allocated object and ensures its proper deletion when it goes out of scope. It has exclusive ownership of the object it manages, meaning no two std::unique_ptr
can own the same object simultaneously.
main.cpp
123456789#include <iostream> #include <memory> int main() { std::unique_ptr<int> first_unique_pointer = std::make_unique<int>(42); // This will cause a compilation error: copying is not allowed std::unique_ptr<int> second_unique_pointer = first_unique_pointer; }
Moving a Unique Pointer
Although copying is not allowed, you can transfer ownership of the object using std::move
. This moves the resource from one std::unique_ptr
to another, leaving the original pointer empty.
main.cpp
123456789101112131415#include <iostream> #include <memory> int main() { std::unique_ptr<int> first_unique_pointer = std::make_unique<int>(42); // Transferring ownership using std::move std::unique_ptr<int> second_unique_pointer = std::move(first_unique_pointer); if (!first_unique_pointer) std::cout << "first_unique_pointer is now empty.\n"; std::cout << "second_unique_pointer owns the value: " << *second_unique_pointer << '\n'; }
When you use std::move
, ownership of the object is transferred from one unique pointer to another. After this, the original pointer no longer owns the object, and the new pointer becomes its owner.
Swipe to start coding
Complete the following code in a way that the ownership of the dynamically allocated integer (with value 42) is safely transferred from unique_pointer_a
to unique_pointer_b
.
Solution
solution.cpp
Thanks for your feedback!
single
Awesome!
Completion rate improved to 5.56
Moving Unique Pointers
Swipe to show menu
Copying Not Allowed
A std::unique_ptr
is a smart pointer that manages a dynamically allocated object and ensures its proper deletion when it goes out of scope. It has exclusive ownership of the object it manages, meaning no two std::unique_ptr
can own the same object simultaneously.
main.cpp
123456789#include <iostream> #include <memory> int main() { std::unique_ptr<int> first_unique_pointer = std::make_unique<int>(42); // This will cause a compilation error: copying is not allowed std::unique_ptr<int> second_unique_pointer = first_unique_pointer; }
Moving a Unique Pointer
Although copying is not allowed, you can transfer ownership of the object using std::move
. This moves the resource from one std::unique_ptr
to another, leaving the original pointer empty.
main.cpp
123456789101112131415#include <iostream> #include <memory> int main() { std::unique_ptr<int> first_unique_pointer = std::make_unique<int>(42); // Transferring ownership using std::move std::unique_ptr<int> second_unique_pointer = std::move(first_unique_pointer); if (!first_unique_pointer) std::cout << "first_unique_pointer is now empty.\n"; std::cout << "second_unique_pointer owns the value: " << *second_unique_pointer << '\n'; }
When you use std::move
, ownership of the object is transferred from one unique pointer to another. After this, the original pointer no longer owns the object, and the new pointer becomes its owner.
Swipe to start coding
Complete the following code in a way that the ownership of the dynamically allocated integer (with value 42) is safely transferred from unique_pointer_a
to unique_pointer_b
.
Solution
solution.cpp
Thanks for your feedback!