Introduction to move semantics (part 2)
Transferring ownership explicitly with std::move()
In this video, I write zero
in a vector's size field in the memory diagram examples, but for some reason say one
both times. I meant to say zero
, just like I wrote it!
Move semantics for our doubly linked list
Summary and additional considerations
Activity: Move semantics for our naive unique pointer implementation
Graded Playground Autograder
Unique pointer
The mantra of the unique pointer is exclusive ownership and accordingly implements a move-only type. The unique pointer implementation
utilizes the resource acquisition is initialization (RAII) programming idiom, where the lifetime of a dynamically allocated object (i.e., the resource) is tied to the unique pointer object that owns it.
The allocation of the dynamically allocated object (i.e., resource acquisition) owned by the unique pointer happens during construction (initialization). Deallocation of that object is always completed during destruction. A non-null By its name, the unique pointer always owns what it points to. Therefore, two objects of this type will never share the same resource. This invariant is maintained by disabling the copy mechanisms for this type.
In this activity
You will implement move semantics for UniquePtr
.
Member functions you are to implement
public: | |
UniquePtr(UniquePtr<T>&& rhs); |
Move constructor. Transfers ownership of rhs.holder_ to this object (i.e., that on left-hand side of = ). |
UniquePtr<T>& operator=(UniquePtr<T>&& rhs); |
Move assignment operator. Transfers ownership of rhs.holder_ to this object (i.e., that on left-hand side of = ). Ensures no memory is leaked. |
Provided member functions
public: | |
UniquePtr(); |
We will use the default, compiler-generated definition of the default constructor. |
UniquePtr(const T& data); |
Allocates a new dynamically allocated object of type T on the free store that is initialized with a copy of data .
|
UniquePtr(const UniquePtr<T>& rhs); |
delete d. |
UniquePtr<T>& operator=(const UniquePtr<T>& rhs); |
delete d. |
~UniquePtr(); |
Deallocates the dynamically allocated object pointed to by holder_ . |
Data members
private: | |
T* holder_; |
holder_ points to the dynamically allocated object owned by UniquePtr . |