From 4b4605df15ce7061a08d7911069927bf49778d05 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 7 Mar 2022 09:24:37 +0100 Subject: fix: prevent invalid UniquePtr usage --- src/std/memory.hpp | 12 +++++++++--- src/std/memory.tpp | 19 ------------------- 2 files changed, 9 insertions(+), 22 deletions(-) (limited to 'src/std') diff --git a/src/std/memory.hpp b/src/std/memory.hpp index 0c5719c..d1ca762 100644 --- a/src/std/memory.hpp +++ b/src/std/memory.hpp @@ -11,13 +11,19 @@ class UniquePtr public: explicit UniquePtr() = default; explicit UniquePtr(Target *target); - UniquePtr(const UniquePtr &unique_ptr); - UniquePtr(UniquePtr &&unique_ptr) noexcept; - UniquePtr &operator=(const UniquePtr &unique_ptr); + // Move constructor + UniquePtr(UniquePtr &&unique_ptr) noexcept; + // Move assignment operator UniquePtr &operator=(UniquePtr &&unique_ptr) noexcept; + // Disable the copy constructor + UniquePtr(const UniquePtr &unique_ptr) = delete; + + // Disable the copy assignment operator + UniquePtr &operator=(const UniquePtr &unique_ptr) = delete; + ~UniquePtr(); Target operator*() const; diff --git a/src/std/memory.tpp b/src/std/memory.tpp index fdfe72b..b2f39bf 100644 --- a/src/std/memory.tpp +++ b/src/std/memory.tpp @@ -26,12 +26,6 @@ UniquePtr::UniquePtr(Target *target) : _target(target) { } -template -UniquePtr::UniquePtr(const UniquePtr &unique_ptr) - : _target(new Target(*(unique_ptr._target))) -{ -} - template UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept : _target(unique_ptr._target) @@ -39,19 +33,6 @@ UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept unique_ptr._target = nullptr; } -template -UniquePtr &UniquePtr::operator=(const UniquePtr &unique_ptr) -{ - if (&unique_ptr != this) - { - delete _target; - _target = nullptr; - _target = new Target(*(unique_ptr._target)); - } - - return *this; -} - template UniquePtr &UniquePtr::operator=(UniquePtr &&unique_ptr) noexcept { -- cgit v1.2.3-18-g5258