From 5e9cb4ddc811465731596d8ee684f3d8cd5a6aea Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 2 May 2026 16:05:06 +0300 Subject: feat(Week11): Add solutions --- Week11/Exercise06/UniquePointer.hpp | 75 +++++++++++++++++++++++++++++++++++++ Week11/Exercise06/main.cpp | 9 +++++ 2 files changed, 84 insertions(+) create mode 100644 Week11/Exercise06/UniquePointer.hpp create mode 100644 Week11/Exercise06/main.cpp (limited to 'Week11/Exercise06') 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 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 +UniquePointer::UniquePointer(T* value) : value(value) +{ } + +template +UniquePointer::UniquePointer() : value() +{ } + +template +UniquePointer::~UniquePointer() { + delete value; +} + +template +UniquePointer::UniquePointer(UniquePointer&& other) { + value = other.value; + + other.value = nullptr; +} + +template +UniquePointer& UniquePointer::operator=(UniquePointer&& other) { + if (this != &other) { + delete value; + + value = other.value; + + other.value = nullptr; + } + return *this; +} + +template +T& UniquePointer::operator*() { + return *value; +} + +template +const T& UniquePointer::operator*() const { + return *value; +} + +template +T* UniquePointer::operator->() { + return value; +} + +template +const T* UniquePointer::operator->() const { + return value; +} + +#endif /* _UNIQUEPOINTER */ diff --git a/Week11/Exercise06/main.cpp b/Week11/Exercise06/main.cpp new file mode 100644 index 0000000..8853a90 --- /dev/null +++ b/Week11/Exercise06/main.cpp @@ -0,0 +1,9 @@ +#include "UniquePointer.hpp" +#include + +int main() { + UniquePointer ptr(new int); + // Този ред, без "std::move", ще хвърли грешка, + // понеже нямаме копиращи семантики + UniquePointer ptr1 = std::move(ptr); +} -- cgit v1.3.1-23-gd82d