Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<cstddef>
- using namespace std;
- template<typename T>
- class UniquePtr {
- private:
- T *ptr;
- public:
- UniquePtr() {
- ptr = new T();
- }
- explicit UniquePtr(T *other) {
- ptr = other;
- }
- UniquePtr(UniquePtr &) = delete;
- UniquePtr(UniquePtr &&other) noexcept {
- ptr = other.ptr;
- other.ptr = nullptr;
- }
- UniquePtr &operator=(nullptr_t &) {
- if (ptr)
- delete ptr;
- ptr = nullptr;
- }
- UniquePtr &operator=(UniquePtr &&other) noexcept {
- if (ptr)
- delete ptr;
- ptr = other.ptr;
- }
- UniquePtr &operator=(UniquePtr &other) = delete;
- T operator*() const {
- return *ptr;
- }
- T *operator->() const {
- return ptr;
- }
- T *release() {
- if (ptr)
- delete ptr;
- return ptr;
- }
- void reset(T *other) {
- if (ptr)
- delete ptr;
- ptr = other;
- }
- void swap(UniquePtr &other) {
- swap(ptr, other.ptr);
- }
- T *get() const {
- return ptr;
- }
- explicit operator bool() const {
- return (ptr == nullptr);
- }
- ~UniquePtr() {
- delete ptr;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement