智能指针(smart pointer)就是把动态分配的内存包装在一个基于栈的变量中。引用计数是用来跟踪多少个智能指针指向某个实际指针。
Code
#include <map>
#include <iostream>
template <typename T>
class SuperSmartPointer
{
public:
explicit SuperSmartPointer(T* inPtr);
~SuperSmartPointer();
SuperSmartPointer(const SuperSmartPointer<T>& src);
SuperSmartPointer<T>& operator=(const SuperSmartPointer<T>& rhs);
const T& operator*() const;
const T* operator->() const;
T& operator*();
T* operator->();
operator void*() const { return mPtr; }
protected:
T* mPtr;
static std::map<T*, int> sRefCountMap;
void finalizePointer();
void initPointer(T* inPtr);
};
template <typename T>
std::map<T*, int>SuperSmartPointer<T>::sRefCountMap;
template <typename T>
SuperSmartPointer<T>::SuperSmartPointer(T* inPtr)
{
initPointer(inPtr);
}
template <typename T>
SuperSmartPointer<T>::SuperSmartPointer(const SuperSmartPointer<T>& src)
{
initPointer(src.mPtr);
}
template <typename T>
SuperSmartPointer<T>&
SuperSmartPointer<T>::operator=(const SuperSmartPointer<T>& rhs)
{
if (this == &rhs) {
return (*this);
}
finalizePointer();
initPointer(rhs.mPtr);
return (*this);
}
template <typename T>
SuperSmartPointer<T>::~SuperSmartPointer()
{
finalizePointer();
}
template<typename T>
void SuperSmartPointer<T>::initPointer(T* inPtr)
{
mPtr = inPtr;
if (sRefCountMap.find(mPtr) == sRefCountMap.end()) {
sRefCountMap[mPtr] = 1;
} else {
sRefCountMap[mPtr]++;
}
}
template<typename T>
void SuperSmartPointer<T>::finalizePointer()
{
if (sRefCountMap.find(mPtr) == sRefCountMap.end()) {
std::cerr << "ERROR: Missing entry in map!" << std::endl;
return;
}
sRefCountMap[mPtr]--;
if (sRefCountMap[mPtr] == 0) {
// no No more references to this object -- delete it and remove from map
sRefCountMap.erase(mPtr);
delete mPtr;
}
}
template <typename T>
const T* SuperSmartPointer<T>::operator->() const
{
return (mPtr);
}
template <typename T>
const T& SuperSmartPointer<T>::operator*() const
{
return (*mPtr);
}
template <typename T>
T* SuperSmartPointer<T>::operator->()
{
return (mPtr);
}
template <typename T>
T& SuperSmartPointer<T>::operator*()
{
return (*mPtr);
}
// Tests follow
class Nothing
{
public:
Nothing() { sNumAllocations++; }
~Nothing() { sNumDeletions++; }
static int sNumAllocations;
static int sNumDeletions;
};
int Nothing::sNumAllocations = 0;
int Nothing::sNumDeletions = 0;
int main(int argc, char** argv)
{
Nothing* myNothing = new Nothing();
{
SuperSmartPointer<Nothing> ptr1(myNothing);
SuperSmartPointer<Nothing> ptr2(myNothing);
}
if (Nothing::sNumAllocations != Nothing::sNumDeletions) {
std::cout << "TEST FAILED: " << Nothing::sNumAllocations <<
" allocations and " << Nothing::sNumDeletions <<
" deletions" << std::endl;
} else {
std::cout << "TEST PASSED" << std::endl;
}
}
该智能指针实现存在一些问题: 因为是通过模板类实现的,所以对于SuperSmartPointer<int>和SuperSmartPoint<char>在编译时
会生成2个类,一般是没有问题的,但是下面的情况:
char * ch = new char;
SuperSmartPoint<char> t1(ch);
SuperSmartPoint<int> t2(int(*)ch);
由于对应2个不同的智能指针映射对应一个实体ch,会出现2次删除的现象。而且该智能指针不是线程安全的。