-
-
Save Glavak/337ef1202bac22e08f943e92f3eb597a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by glavak on 23.09.16. | |
// | |
#ifndef MYARRAY_SMARTPOINTER_H | |
#define MYARRAY_SMARTPOINTER_H | |
#include <utility> | |
template<class T> | |
class SmartPointer | |
{ | |
public: | |
SmartPointer(T * obj) | |
{ | |
this->obj = obj; | |
} | |
SmartPointer() | |
{ | |
this->obj = nullptr; | |
} | |
// Copy constructor | |
SmartPointer(SmartPointer & other) | |
{ | |
this->obj = other.get(); | |
other.release(); | |
} | |
~SmartPointer() | |
{ | |
delete obj; | |
} | |
// Copy assignment | |
SmartPointer & operator=(SmartPointer & other) | |
{ | |
if(&other != this) | |
{ | |
reset(other.get()); | |
other.release(); | |
} | |
return *this; | |
} | |
T * operator->() const | |
{ | |
return obj; | |
} | |
T & operator*() const | |
{ | |
return *obj; | |
} | |
explicit operator bool() const | |
{ | |
return obj != nullptr; | |
} | |
T * get() const | |
{ | |
return obj; | |
} | |
T * release() | |
{ | |
T * res = obj; | |
obj = nullptr; | |
return res; | |
} | |
void reset(T * obj) | |
{ | |
delete this->obj; | |
this->obj = obj; | |
} | |
void swap(SmartPointer & other) | |
{ | |
std::swap(this->obj, other.obj); | |
} | |
private: | |
T * obj; | |
}; | |
#endif //MYARRAY_SMARTPOINTER_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment