Kylin/DataStructure/KylinSmartPointer.h
2023-12-27 10:29:16 +08:00

37 lines
870 B
C++

#ifndef DATA_SMARTPOINTER_H
#define DATA_SMARTPOINTER_H
#include "Pointer.h"
namespace Kylin {
template <typename T>
class SmartPointer : public Pointer<T> {
public:
SmartPointer(T *p) { this->m_pointer = p; }
SmartPointer(SmartPointer &&other) {
this->m_pointer = other.m_pointer;
other.m_pointer = nullptr;
}
SmartPointer(const SmartPointer &) = delete;
~SmartPointer() {
if (this->m_pointer != nullptr) {
delete this->m_pointer;
}
}
SmartPointer &operator=(SmartPointer &&other) {
if (this != &other) {
if (this->m_pointer != nullptr) delete this->m_pointer;
this->m_pointer = other.m_pointer;
other.m_pointer = nullptr;
}
return *this;
}
};
} // namespace Kylin
#endif // SMARTPOINTER_H