65,211
社区成员
发帖
与我相关
我的任务
分享
#ifndef __XLPOINTER_H_B0788703_ABD1_457D_8FEC_E527581FD9EF_INCLUDED__
#define __XLPOINTER_H_B0788703_ABD1_457D_8FEC_E527581FD9EF_INCLUDED__
namespace xl
{
#ifndef NULL
#define NULL 0
#endif
/// @brief Smart Pointer.
template <typename T>
class QIPtr
{
public:
/**
* @brief Default constructor.
*/
QIPtr();
/**
* @brief Constructor. Must give an heap address. Sample use: QIPtr<int> p = new int;.
* @param pData [in] A heap address, usually returned by operator new.
* @remark operator delete must not be called, if using QIPtr.
*/
QIPtr(T *pData);
/**
* @brief Copy construction.
* @param that [in] The pointer to be copied.
*/
QIPtr(const QIPtr<T> &that);
/**
* @brief Destroyer. Inside this function, the heap address will be released if there is no more references.
*/
~QIPtr();
public:
/**
* @brief Operator *, use it as usual.
* @return return a reference of T-typed object.
*/
T &operator*() const;
/**
* @brief Operator ->, use it as usual.
* @return return the address of the object.
*/
T *operator->() const;
/**
* @brief Copy operator, use it as usual.
* @param that [in] The pointer to be copied.
* @return Reference of this object
*/
QIPtr<T> &operator=(const QIPtr<T> &that);
/**
* @brief Copy operator, use it as usual.
* @param that [in] The pointer to be copied.
* @return Return true if the two points equals, return false otherwise.
*/
bool operator==(const QIPtr<T> &that) const;
/**
* @brief Copy operator, use it as usual.
* @param that [in] The pointer to be copied.
* @return Return true if the two points do not equals, return false otherwise.
*/
bool operator!=(const QIPtr<T> &that) const;
private:
// Do not use these functions.
private:
T *m_pData;
size_t *m_pcRefs;
};
template <typename T>
QIPtr<T>::QIPtr()
{
this->m_pData = NULL;
this->m_pcRefs = new size_t;
*this->m_pcRefs = 0;
}
template <typename T>
inline QIPtr<T>::QIPtr(T *pData)
{
this->m_pData = pData;
this->m_pcRefs = new size_t;
*this->m_pcRefs = 1;
}
template <typename T>
QIPtr<T>::QIPtr(const QIPtr<T> &that)
{
this->m_pData = that.m_pData;
this->m_pcRefs = that.m_pcRefs;
++*this->m_pcRefs;
}
template <typename T>
QIPtr<T>::~QIPtr()
{
if (--*this->m_pcRefs == 0)
{
delete this->m_pcRefs;
if (this->m_pData != NULL)
{
delete this->m_pData;
}
}
}
template <typename T>
inline T &QIPtr<T>::operator*() const
{
return *this->m_pData;
}
template <typename T>
inline T *QIPtr<T>::operator->() const
{
return this->m_pData;
}
template <typename T>
QIPtr<T> &QIPtr<T>::operator=(const QIPtr<T> &that)
{
if (this == &that)
{
return *this;
}
if (this->m_pData == that.m_pData)
{
++*this->m_pcRefs;
}
else
{
this->~QIPtr();
this->m_pData = that.m_pData;
this->m_pcRefs = that.m_pcRefs;
++*this->m_pcRefs;
}
return *this;
}
template <typename T>
bool QIPtr<T>::operator==(const QIPtr<T> &that) const
{
return this->m_pData == that.m_pData;
}
template <typename T>
bool QIPtr<T>::operator!=(const QIPtr<T> &that) const
{
return this->m_pData != that.m_pData;
}
} // namespace xl
#endif // #ifndef __XLPOINTER_H_B0788703_ABD1_457D_8FEC_E527581FD9EF_INCLUDED__
#ifndef __DEBUGNEW_H_33164B60_A660_4B88_AEA3_270C97F24B12_INCLUDED__
#define __DEBUGNEW_H_33164B60_A660_4B88_AEA3_270C97F24B12_INCLUDED__
#ifdef __cplusplus
#ifdef _DEBUG
#include <crtdbg.h>
// Overload operator new
inline void * __cdecl operator new(size_t nSize, const char * lpszFileName, int nLine)
{
int nFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
nFlag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(nFlag);
return ::operator new(nSize, _NORMAL_BLOCK, lpszFileName, nLine);
};
#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW
// Overload operator delete (, or the compiler will give warning.)
inline void __cdecl operator delete(void *pData, const char * lpszFileName, int nLine)
{
::operator delete(pData);
}
#endif // #ifdef _DEBUG
#endif // #ifdef __cplusplus
#endif // #ifndef __DEBUGNEW_H_33164B60_A660_4B88_AEA3_270C97F24B12_INCLUDED__
#include "DebugNew.h"
//#include <assert.h>
#include "xlPointer.h"
using namespace xl;
int main()
{
QIPtr<int> c = new int;
*c = 3;
QIPtr<int> b = c;
*b = 8;
b = new int;
*b = 4;
*c = 8;
int d = *b;
return 0;
}
#ifndef __XLQIPTR_H_B0788703_ABD1_457D_8FEC_E527581FD9EF_INCLUDED__
#define __XLQIPTR_H_B0788703_ABD1_457D_8FEC_E527581FD9EF_INCLUDED__
namespace xl
{
#ifndef NULL
#define NULL 0
#endif
/// @brief Smart Pointer.
template <typename T>
class QIPtr
{
public:
/**
* @brief Default constructor.
*/
QIPtr();
/**
* @brief Constructor. Must give an heap address. Sample use: QIPtr<int> p(new int);.
* @param pData [in] A heap address, usually returned by operator new.
* @remark operator delete must not be called, if using QIPtr.
*/
explicit QIPtr(T *pData);
/**
* @brief Copy construction.
* @param that [in] The pointer to be copied.
*/
QIPtr(const QIPtr<T> &that);
/**
* @brief Destroyer. Inside this function, the heap address will be released if there is no more references.
*/
~QIPtr();
public:
/**
* @brief Operator *, use it as usual.
* @return return a reference of T-typed object.
*/
T &operator*() const;
/**
* @brief Operator ->, use it as usual.
* @return return the address of the object.
*/
T *operator->() const;
/**
* @brief Copy operator, use it as usual.
* @param that [in] The pointer to be copied.
* @return Reference of this object
*/
QIPtr<T> &operator=(const QIPtr<T> &that);
/**
* @brief Compare operator, use it as usual.
* @param that [in] The pointer to be compared.
* @return Return true if the two points equals, return false otherwise.
*/
bool operator==(const QIPtr<T> &that) const;
/**
* @brief Compare operator, use it as usual.
* @param that [in] The pointer to be compared.
* @return Return true if the two points do not equals, return false otherwise.
*/
bool operator!=(const QIPtr<T> &that) const;
private:
void AddRef();
void Release();
private:
T *m_pData;
size_t *m_pcRefs;
};
template <typename T>
inline void QIPtr<T>::AddRef()
{
if (this->m_pcRefs == NULL)
{
this->m_pcRefs = new size_t;
*this->m_pcRefs = 0;
}
++*this->m_pcRefs;
}
template <typename T>
inline void QIPtr<T>::Release()
{
if (this->m_pcRefs == NULL)
{
return;
}
if (--*this->m_pcRefs > 0)
{
return;
}
delete this->m_pcRefs;
if (this->m_pData == NULL)
{
return;
}
delete this->m_pData;
}
template <typename T>
inline QIPtr<T>::QIPtr() : m_pData(NULL), m_pcRefs(NULL)
{
}
template <typename T>
inline QIPtr<T>::QIPtr(T *pData) : m_pData(NULL), m_pcRefs(NULL)
{
this->m_pData = pData;
this->AddRef();
}
template <typename T>
inline QIPtr<T>::QIPtr(const QIPtr<T> &that) : m_pData(NULL), m_pcRefs(NULL)
{
this->m_pData = that.m_pData;
this->m_pcRefs = that.m_pcRefs;
this->AddRef();
}
template <typename T>
inline QIPtr<T>::~QIPtr()
{
this->Release();
}
template <typename T>
inline T &QIPtr<T>::operator*() const
{
return *this->m_pData;
}
template <typename T>
inline T *QIPtr<T>::operator->() const
{
return this->m_pData;
}
template <typename T>
inline QIPtr<T> &QIPtr<T>::operator=(const QIPtr<T> &that)
{
//if (this == &that)
//{
// return *this;
//}
if (this->m_pData == that.m_pData)
{
return *this;
}
this->Release();
this->m_pData = that.m_pData;
this->m_pcRefs = that.m_pcRefs;
this->AddRef();
return *this;
}
template <typename T>
bool QIPtr<T>::operator==(const QIPtr<T> &that) const
{
return this->m_pData == that.m_pData;
}
template <typename T>
bool QIPtr<T>::operator!=(const QIPtr<T> &that) const
{
return this->m_pData != that.m_pData;
}
} // namespace xl
#endif // #ifndef __XLQIPTR_H_B0788703_ABD1_457D_8FEC_E527581FD9EF_INCLUDED__
template <typename T>
QIPtr<T> &QIPtr<T>::operator=(const QIPtr<T> &that)
{
if (this == &that)
{
return *this;
}
if (this->m_pData == that.m_pData)
{
++*this->m_pcRefs;
}
else
{
this->~QIPtr();
this->m_pData = that.m_pData;
this->m_pcRefs = that.m_pcRefs;//应为复制过去了,指针的个数也就过去了
++*this->m_pcRefs;//这句的就不应该要了
}
return *this;
}
class sample1
{
int *a;
public:
sample1()
{
a = new int;
}
virtual ~sample1()
{
delete a;
}
};
class sample2 : public sample1
{
int *b;
public:
sample2()
{
b = new int;
}
~sample2()
{
delete b;
}
};
QIPtr<sample1> x = new sample2;