[AD] -- 下方为内容广告,点击支持作者,想过滤广告? -- [AD]
「代码片段」 unique_ptr
#include <iostream>
template <typename T>
class unique_ptr {
public:
unique_ptr(T* ptr = nullptr) : ptr(ptr) {}
~unique_ptr() {
delete ptr;
}
unique_ptr(unique_ptr&& other) noexcept : ptr(other.ptr) {
other.ptr = nullptr;
}
unique_ptr& operator=(unique_ptr&& other) noexcept {
if (this!= &other) {
delete ptr;
ptr = other.ptr;
other.ptr = nullptr;
}
return *this;
}
T& operator*() const {
return *ptr;
}
T* operator->() const {
return ptr;
}
T* release() {
T* temp = ptr;
ptr = nullptr;
return temp;
}
private:
T* ptr;
unique_ptr(const unique_ptr&) = delete;
unique_ptr& operator=(const unique_ptr&) = delete;
};
[AD] -- 下方为内容广告,点击支持作者,想过滤广告? -- [AD]