Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstddef>
- using namespace std;
- template <typename T>
- class UniquePtr {
- private:
- T* obj;
- public:
- // +
- explicit UniquePtr() {
- obj = nullptr;
- }
- // +
- explicit UniquePtr(T* link): obj(link) {
- link = nullptr;
- }
- // +
- UniquePtr(UniquePtr&& a) {
- obj = a.release();
- }
- // +
- UniquePtr& operator=(nullptr_t) {
- obj = nullptr;
- return *this;
- }
- // +
- UniquePtr& operator=(UniquePtr&& a) {
- obj = a.release();
- return *this;
- }
- // +
- T& operator* () {
- return *obj;
- }
- // +
- T& operator* () const {
- return *obj;
- }
- // +
- T* operator-> () {
- return obj;
- }
- // +
- T* operator-> () const {
- return obj;
- }
- // +
- T* release() {
- T* timeObj = obj;
- obj = nullptr;
- return timeObj;
- }
- // +
- void reset(T* ptr) {
- obj = ptr;
- ptr = nullptr;
- }
- // +
- void swap(UniquePtr& other) {
- T* timeObj = obj;
- obj = other.get();
- other.reset(timeObj);
- }
- // +
- T* get() const {
- return obj;
- }
- // +
- explicit operator bool() {
- return (obj != 0);
- }
- // +
- ~UniquePtr() {
- delete obj;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement