Website Summary Files Commits Branches @commit @diff @stats
path: root / Week11 / Exercise03
diff options
context:
space:
mode:
Commit5e9cb4ddc811465731596d8ee684f3d8cd5a6aea (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-05-02 16:05:06 +0300
Committer
 Syndamia <kamen@syndamia.com>
2026-05-02 16:05:06 +0300
Files33a9268d1136974d2ef138364dde508ba43b4a52 /Week11/Exercise03
oop-2025-solutions-main.tar
oop-2025-solutions-main.tar.gz
oop-2025-solutions-main.zip
Parent7546f0d0da487e16f0a8671c09a46e1cee2bc03a (diff)
feat(Week11): Add solutions HEAD main
2 files changed, 146 insertions, 0 deletions
-rw-r--r--Week11/Exercise03/SharedPointer.hpp129
-rw-r--r--Week11/Exercise03/main.cpp17
diff --git a/Week11/Exercise03/SharedPointer.hpp b/Week11/Exercise03/SharedPointer.hpp
new file mode 100644
index 0000000..06f9bbb
--- /dev/null
+++ b/Week11/Exercise03/SharedPointer.hpp
@@ -0,0 +1,129 @@
+#ifndef _SHAREDPOINTER
+#define _SHAREDPOINTER
+
+template<class T>
+class SharedPointer {
+ T* value;
+ unsigned* instances;
+
+ void free();
+ void copyFrom(const SharedPointer& other);
+
+public:
+ SharedPointer(T* value);
+
+ SharedPointer();
+ ~SharedPointer();
+ SharedPointer(const SharedPointer& other);
+ SharedPointer& operator=(const SharedPointer& other);
+
+ SharedPointer(SharedPointer&& other);
+ SharedPointer& operator=(SharedPointer&& other);
+
+ T& operator*();
+ const T& operator*() const;
+ T* operator->();
+ const T* operator->() const;
+
+ unsigned RefCount() const;
+};
+
+template <class T>
+void SharedPointer<T>::free() {
+ if (instances == nullptr)
+ return;
+
+ --*instances;
+ if (*instances == 0) {
+ delete instances;
+ delete value;
+ }
+}
+
+template <class T>
+void SharedPointer<T>::copyFrom(const SharedPointer& other) {
+ this->instances = other.instances;
+ this->value = other.value;
+ ++*instances;
+}
+
+template <class T>
+SharedPointer<T>::SharedPointer(T* value) : instances(new unsigned), value(value)
+{
+ *instances = 1;
+}
+
+template <class T>
+SharedPointer<T>::SharedPointer() : instances(), value()
+{ }
+
+template <class T>
+SharedPointer<T>::~SharedPointer() {
+ free();
+}
+
+template <class T>
+SharedPointer<T>::SharedPointer(const SharedPointer& other) {
+ copyFrom(other);
+}
+
+template <class T>
+SharedPointer<T>& SharedPointer<T>::operator=(const SharedPointer& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+template <class T>
+SharedPointer<T>::SharedPointer(SharedPointer&& other) {
+ instances = other.instances;
+ value = other.value;
+
+ other.instances = nullptr;
+ other.value = nullptr;
+}
+
+template <class T>
+SharedPointer<T>& SharedPointer<T>::operator=(SharedPointer&& other) {
+ if (this != &other) {
+ free();
+
+ instances = other.instances;
+ value = other.value;
+
+ other.instances = nullptr;
+ other.value = nullptr;
+ }
+ return *this;
+}
+
+template <class T>
+T& SharedPointer<T>::operator*() {
+ return *value;
+}
+
+template <class T>
+const T& SharedPointer<T>::operator*() const {
+ return *value;
+}
+
+template <class T>
+T* SharedPointer<T>::operator->() {
+ return value;
+}
+
+template <class T>
+const T* SharedPointer<T>::operator->() const {
+ return value;
+}
+
+template <class T>
+unsigned SharedPointer<T>::RefCount() const {
+ if (instances == nullptr)
+ return 0;
+ return *instances;
+}
+
+#endif /* _SHAREDPOINTER */
diff --git a/Week11/Exercise03/main.cpp b/Week11/Exercise03/main.cpp
new file mode 100644
index 0000000..d8a4d66
--- /dev/null
+++ b/Week11/Exercise03/main.cpp
@@ -0,0 +1,17 @@
+#include "SharedPointer.hpp"
+#include <iostream>
+
+void inc(SharedPointer<int> val) {
+ (*val)++;
+}
+
+int main() {
+ SharedPointer<int> x(new int);
+ SharedPointer<int> y = x;
+ *x = 5;
+ std::cout << *x << ' ' << *y << std::endl;
+ inc(y);
+ std::cout << *x << ' ' << *y << std::endl;
+
+ return 0;
+}