12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- #ifndef UNIQUEPOINTER_H
- #define UNIQUEPOINTER_H
- template<typename T>
- class UniquePointer {
- T* t;
- public:
- UniquePointer(T* t) : t(t) {
- }
- UniquePointer() : UniquePointer(nullptr) {
- }
- ~UniquePointer() {
- delete t;
- }
- UniquePointer(const UniquePointer&) = delete;
- UniquePointer& operator=(const UniquePointer&) = delete;
- UniquePointer(UniquePointer&& other) : UniquePointer(other.t) {
- other.t = nullptr;
- }
- UniquePointer& operator=(UniquePointer&& other) {
- if(this != &other) {
- delete t;
- t = other.t;
- other.t = nullptr;
- }
- return *this;
- };
- T* operator->() {
- return t;
- }
- const T* operator->() const {
- return t;
- }
- operator T*() {
- return t;
- }
- operator const T*() const {
- return t;
- }
- };
- #endif
|