Skip to content

Unique ptr #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: unique_ptr
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions homework/unique_ptr/unique_ptr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "unique_ptr.hpp"

namespace my {

unique_ptr::unique_ptr(T* ptr){};

unique_ptr::~unique_ptr() {
if (!ptr_) {
delete ptr_;
}
};

unique_ptr::unique_ptr(unique_ptr&& other) {
if (!ptr_) {
delete ptr_;
}
T* ptr = other.release();
ptr_ = ptr;
}

unique_ptr::unique_ptr& operator=(const unique_ptr& other) = delete;

unique_ptr& unique_ptr::operator=(unique_ptr&& other) {
if (!ptr_) {
delete ptr_;
}
T* ptr = other.release();
ptr_ = ptr;
}

T& unique_ptr::operator*() {
return *ptr_;
};

T* unique_ptr::operator->() {
return ptr_;
};

T* unique_ptr::get() const { return ptr_ };

T* unique_ptr::release() {
T* ptr = ptr_;
ptr_ = nullptr;
return ptr;
};

void unique_ptr::reset(T* ptr) {
if (!ptr_) {
delete ptr_;
}
};

} // namespace my
25 changes: 25 additions & 0 deletions homework/unique_ptr/unique_ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@


namespace my {

template <typename T>
class unique_ptr {
public:
unique_ptr(T* ptr)
: ptr_(ptr);
unique_ptr(const unique_ptr&);
~unique_ptr();
unique_ptr(unique_ptr&& other);
unique_ptr& operator=(const unique_ptr& other) = delete;
unique_ptr& operator=(unique_ptr&& other);
T& operator*();
T* operator->();
T* get() const;
T* release();
void reset(T* ptr);

private:
T* ptr_;
};

}; // namespace my
Loading