diff options
| Commit | 5e9cb4ddc811465731596d8ee684f3d8cd5a6aea (patch) | |
|---|---|---|
| Author | 2026-05-02 16:05:06 +0300 | |
| Committer | 2026-05-02 16:05:06 +0300 | |
| Files | 33a9268d1136974d2ef138364dde508ba43b4a52 /Week11/Exercise06/UniquePointer.hpp | |
| oop-2025-solutions-main.tar oop-2025-solutions-main.tar.gz oop-2025-solutions-main.zip | ||
| Parent | 7546f0d0da487e16f0a8671c09a46e1cee2bc03a (diff) | |
1 files changed, 75 insertions, 0 deletions
| -rw-r--r-- | Week11/Exercise06/UniquePointer.hpp | 75 |
diff --git a/Week11/Exercise06/UniquePointer.hpp b/Week11/Exercise06/UniquePointer.hpp new file mode 100644 index 0000000..fe95234 --- /dev/null +++ b/Week11/Exercise06/UniquePointer.hpp @@ -0,0 +1,75 @@ +#ifndef _UNIQUEPOINTER +#define _UNIQUEPOINTER + +template<class T> +class UniquePointer { + T* value; + +public: + UniquePointer(T* value); + + UniquePointer(); + ~UniquePointer(); + + UniquePointer(UniquePointer&& other); + UniquePointer& operator=(UniquePointer&& other); + + T& operator*(); + const T& operator*() const; + T* operator->(); + const T* operator->() const; +}; + +template<class T> +UniquePointer<T>::UniquePointer(T* value) : value(value) +{ } + +template<class T> +UniquePointer<T>::UniquePointer() : value() +{ } + +template<class T> +UniquePointer<T>::~UniquePointer() { + delete value; +} + +template<class T> +UniquePointer<T>::UniquePointer(UniquePointer&& other) { + value = other.value; + + other.value = nullptr; +} + +template<class T> +UniquePointer<T>& UniquePointer<T>::operator=(UniquePointer&& other) { + if (this != &other) { + delete value; + + value = other.value; + + other.value = nullptr; + } + return *this; +} + +template<class T> +T& UniquePointer<T>::operator*() { + return *value; +} + +template<class T> +const T& UniquePointer<T>::operator*() const { + return *value; +} + +template<class T> +T* UniquePointer<T>::operator->() { + return value; +} + +template<class T> +const T* UniquePointer<T>::operator->() const { + return value; +} + +#endif /* _UNIQUEPOINTER */ |
