我有一個unique_ptr
的簡化示例。我想知道在獨特指針中需要移動構造函數和移動賦值操作符嗎? 如果我理解移動構造函數(並傳遞右值),這兩行代碼應該相同。什麼是需要在Unique Pointers中移動構造函數和移動賦值構造函數?
UniquePointer<T> a(new T);
UniquePointer<T> a(UniquePointer<T>(new T));
這裏是簡化UniquePointer
代碼:
template<typename T> class UniquePointer {
T* m_ptr;
public:
UniquePointer(const UniquePointer&) = delete;
UniquePointer& operator=(const UniquePointer&) = delete;
UniquePointer(UniquePointer&& rhs);
UniquePointer& operator=(UniquePointer&& rhs);
UniquePointer(T* ptr) : m_ptr(ptr) { }
T* operator->() const { return m_ptr; }
T& operator*() const { return *m_ptr; }
T* get() const { return m_ptr; }
~UniquePointer() { delete m_ptr; }
};
回訪所有權:汽車獨特=的std :: make_unique() –
不支持移動語義,你不能把它的向量 – sp2danny