Unique Pointers
You need to include memory
The templated class unique_ptr
unique_ptr<type> var; // a pointer to a single item
unique_ptr<type[]> var; // a pointer to a and array.
Generally, these are constructed with make_unique
This is a non-member function.
There are several versions of this.
data = make_unique<int>();
data1 = make_unique<int>(7);
data2 = make_unique<int[]>(size);
They act like pointers or arrays
- * and -> are defined for single element types
- [] is defined for arrays.
The move function is required to copy one.
get
gets the underlying pointer.
- Careful here, please don't undo the underlying
Look at uniqueTest.cpp.
An application
- I have written a basic vector class.
- Using a unique_ptr as the basis.
- note the overloading of the [] operator.
- The const version is for the RHS of an assignment
- The non- const version is for the LHS of an assignment