//SmartPointer.h
#ifndef _SMART_POINTER_H_
#define _SMART_POINTER_H_
template <typename T>
class SmartPointer
{
public:
SmartPointer();
SmartPointer(const T* pointer);
~SmartPointer();
T* operator->();
T& operator*();
bool operator==(const SmartPointer<T>& other);
bool operator!=(const SmartPointer<T>& other);
private:
T* m_pointer;
};
#endif
//SmartPointer.hpp
#ifndef _SMART_POINTER_HPP_
#define _SMART_POINTER_HPP_
#include "SmartPointer.h"
template <typename T>
SmartPointer<T>::SmartPointer()
{
m_pointer = 0;
}
template <typename T>
SmartPointer<T>::SmartPointer(const T *pointer)
{
m_pointer = const_cast<T*>(pointer);
//std::cout<< "SmartPointer(const T *pointer)"<<std::endl;
}
template <typename T>
SmartPointer<T>::~SmartPointer()
{
if (m_pointer != NULL)
{
delete m_pointer;
m_pointer = NULL;
}
//std::cout<< "~SmartPointer"<<std::endl;
}
template <typename T>
T* SmartPointer<T>::operator->()
{
return m_pointer;
}
template <typename T>
T& SmartPointer<T>::operator*()
{
return *m_pointer;
}
template <typename T>
bool SmartPointer<T>::operator==(const SmartPointer<T> &other)
{
bool ret;
if (m_pointer == other.m_pointer)
{
ret = true;
}
else
{
ret = false;
}
return ret;
}
template <typename T>
bool SmartPointer<T>::operator!=(const SmartPointer<T> &other)
{
return !SmartPointer<T>::operator==(other);
}
#endif