智能指针(smart pointer)是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动态分配的对象,防止内存泄露。它的一种通用实现技术是使用引用计数(reference count)。智能指针类将一个计数器与类指向的对象相关联,引用计数跟踪该类有多少个对象共享同一指针。每次创建类的新对象时,初始化指针并将引用计数置为1;当对象作为另一对象的副本而创建时,拷贝构造函数拷贝指针并增加与之相应的引用计数;对一个对象进行赋值时,赋值操作符减少左操作数所指对象的引用计数(如果引用计数为减至0,则删除对象),并增加右操作数所指对象的引用计数;调用析构函数时,构造函数减少引用计数(如果引用计数减至0,则删除基础对象)。
    智能指针就是模拟指针动作的类。所有的智能指针都会重载 -> 和 * 操作符。智能指针还有许多其他功能,比较有用的是自动销毁。这主要是利用栈对象的有限作用域以及临时对象(有限作用域实现)析构函数释放内存。当然,智能指针还不止这些,还包括复制时可以修改源对象等。智能指针根据需求不同,设计也不同(写时复制,赋值即释放对象拥有权限、引用计数等,控制权转移等)。auto_ptr 即是一种常见的智能指针。
     智能指针通常用类模板实现:

template <class T>
class smartpointer
{
private:
T *_ptr;
public:
smartpointer(T *p) : _ptr(p) //构造函数
{
}
T& operator *() //重载*操作符
{
return *_ptr;
}
T* operator ->() //重载->操作符
{
return _ptr;
}
~smartpointer() //析构函数
{
delete _ptr;
}
};

下面我们定义一个Person类:

class Person
{
int age;
char* pName; public:
Person(): pName(),age()
{
}
Person(char* pName, int age): pName(pName), age(age)
{
}
~Person()
{
} void Display()
{
printf("Name = %s Age = %d \n", pName, age);
}
void Shout()
{
printf("Ooooooooooooooooo",);
}
};

这么调用:

void main()
{
SP<PERSON> p(new Person("Scott", 25));
p->Display();
{
SP<PERSON> q = p;
q->Display();
// Destructor of Q will be called here..
}
p->Display();
}
Look what happens here. p and q are referring to the same Person class pointer. Now when q goes out of scope, the destructor of q will be called which deletes the Person class pointer. Now we cannot call p->Display(); since p will be left with a dangling pointer and this call will fail. (Note that this problem would have existed even if we were using normal pointers instead of smart pointers.) We should not delete the Person class pointer unless no body is using it. How do we do that? Implementing a reference counting mechanism in our smart pointer class will solve this problem. What we are going to do is we will have a reference counting class RC. This class will maintain an integer value which represents the reference count. We will have methods to increment and decrement the reference count.
我们写一个RC类:
class RC
{
private:
int count; // Reference count public:
void AddRef()
{
// Increment the reference count
count++;
} int Release()
{
// Decrement the reference count and
// return the reference count.
return --count;
}

Now that we have a reference counting class, we will introduce this to our smart pointer class. We will maintain a pointer to class RC in our SP class and this pointer will be shared for all instances of the smart pointer which refers to the same pointer. For this to happen, we need to have an assignment operator and copy constructor in our SP class.

template < typename T > class SP
{
private:
T* pData; // pointer
RC* reference; // Reference count public:
SP() : pData(), reference()
{
// Create a new reference
reference = new RC();
// Increment the reference count
reference->AddRef();
} SP(T* pValue) : pData(pValue), reference()
{
// Create a new reference
reference = new RC();
// Increment the reference count
reference->AddRef();
} SP(const SP<T>& sp) : pData(sp.pData), reference(sp.reference)
{
// Copy constructor
// Copy the data and reference pointer
// and increment the reference count
reference->AddRef();
} ~SP()
{
// Destructor
// Decrement the reference count
// if reference become zero delete the data
if(reference->Release() == )
{
delete pData;
delete reference;
}
} T& operator* ()
{
return *pData;
} T* operator-> ()
{
return pData;
} SP<T>& operator = (const SP<T>& sp)
{
// Assignment operator
if (this != &sp) // Avoid self assignment
{
// Decrement the old reference count
// if reference become zero delete the old data
if(reference->Release() == )
{
delete pData;
delete reference;
} // Copy the data and reference pointer
// and increment the reference count
pData = sp.pData;
reference = sp.reference;
reference->AddRef();
}
return *this;
}
};

客户端调用:

void main()
{
SP<PERSON> p(new Person("Scott", ));
p->Display();
{
SP<PERSON> q = p;
q->Display();
// Destructor of q will be called here.. SP<PERSON> r;
r = p;
r->Display();
// Destructor of r will be called here..
}
p->Display();
// Destructor of p will be called here
// and person pointer will be deleted
}

When we create a smart pointer p of type Person, the constructor of SP will be called, the data will be stored, and a new RC pointer will be created. The AddRef method of RC is called to increment the reference count to 1. Now SP q = p; will create a new smart pointer q using the copy constructor. Here the data will be copied and the reference will again be incremented to 2. Now r = p; will call the assignment operator to assign the value of p to q. Here also we copy the data and increment the reference count, thus making the count 3. When r and q go out of scope, the destructors of the respective objects will be called. Here the reference count will be decremented, but data will not be deleted unless the reference count becomes zero. This happens only when the destructor of p is called. Hence our data will be deleted only when no body is referring to it.

上面的参考了http://www.codeproject.com/Articles/15351/Implementing-a-simple-smart-pointer-in-c

这篇文章。

http://zh.wikipedia.org/wiki/%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88

http://www.informit.com/articles/article.aspx?p=25264

http://ootips.org/yonat/4dev/smart-pointers.html

http://stackoverflow.com/questions/22585974/smart-pointer-implementation

http://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one

另一篇比较好的文章:

http://blog.csdn.net/hackbuteer1/article/details/7561235

c++ smart pointer的更多相关文章

  1. [CareerCup] 13.8 Smart Pointer 智能指针

    13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...

  2. 理解smart pointer之三:unique_ptr

    unique_ptr最先在boost中被定义,后来被C++标准委员会选中为C++11的feature之一. std::unique_ptr is a smart pointer that retain ...

  3. Why do we need smart pointer and how to implement it.

    Here are two simple questions. Problem A #include <string> include <iostream> using name ...

  4. c++(smart pointer)

    (一)首先对智能指针有一些概念性的了解 **********本部分内容摘自开源中国社区http://my.oschina.net/u/158589/blog/28994******** 1.什么是智能 ...

  5. c/c++ 标准库 智能指针( smart pointer ) 是啥玩意儿

    标准库 智能指针( smart pointer ) 是啥玩意儿 一,为什么有智能指针??? c++程序员需要自己善后自己动态开辟的内存,一旦忘了释放,内存就泄露. 智能指针可以帮助程序员"自 ...

  6. C++ smart pointer智能指针

      在C++中,程序员可以直接操作内存,给编程增加了不少的灵活性.但是灵活性是有代价的,程序员必须负责自己负责释放自己申请的内存,否则就会出现内存泄露.智能指针就是为了解决这个问题而存在的.它和其他指 ...

  7. Effective C++ Item 17 Store newed objects in smart pointer in standalone statements

    If you trying to do multiple things in one statement, you should think carefully abnormal behavior e ...

  8. Smart pointer 智能指针小总结

    Smart pointer line 58之后smart pointer里的计数已经是0,所以会真正释放它引用的对象,调用被引用对象的析构函数.如果继续用指针访问,会出现如下图的内存访问异常.所以说如 ...

  9. smart pointer

    smart pointer是一种abstract data type,它可以模仿指针的行为,而且额外提供了一系列诸如自己主动内存管理.边界检查等特性,这些特性是为了在保证效率的基础上降低因为对指针的不 ...

随机推荐

  1. how tomcat works 读书笔记四 tomcat的默认连接器

    事实上在第三章,就已经有了连接器的样子了,只是那仅仅是一个学习工具,在这一章我们会開始分析tomcat4里面的默认连接器. 连接器 Tomcat连接器必须满足下面几个要求 1 实现org.apache ...

  2. iOS--RunLoop原理介绍

    什么是RunLoop RunLoop从字面上看是运行循环的意思,这一点也不错,它确实就是一个循环的概念,或者准确的说是线程中的循环. 本文一开始就提到有些程序是一个圈,这个圈本质上就是这里的所谓的Ru ...

  3. jQuery循环给某个ID赋值

    1.id名为sl的input框循环赋值 $("input[id=sl]").each(function(){alert(this.value) })

  4. NC V6 nchome文件目录及其作用介绍

    NC V6发布一段时间了,各个NC6.0 nchome文件夹下各个子文件夹内容和作用 ant:存放Apache Ant,用来执行EJB的构建. bin: 存放nc部署和系统监控等命令.configsy ...

  5. Ant工具

    Ant工具 Ant是一种基于Java的build工具.理论上来说,它有些类似于(Unix)C中的make ,但没有make的缺陷.目前的最新版本为:Ant 1.9.4[1] .   Ant的概念 当一 ...

  6. xml中报错,验证是否是xml报错

    1.xml中写入sql有时报错,例如有大于号小于号,要用<![CDATA[                  ]]>扩起来 2.验证xml有错的方式,以浏览器方式打开,如果正常打开,无错. ...

  7. angularjs modal模态框----创建可拖动的指令

    //最近项目中需要将angular-ui-bootstrap中用到的弹出框,使之可拖动,由于源文件中没有实现,需要自己实现指令,以下即为该指令,亲测可以实现..directive('draggable ...

  8. Fluent NHibernate之旅

    Fluent NHibernate 之旅 导航篇: [原创]Fluent NHibernate之旅开篇: [原创]Fluent NHibernate之旅二--Entity Mapping: [原创]F ...

  9. DGV属性

    1.控件的SelectedCells.Count属性可以判断用户是否已经选择数据,如果大于0说明有选择的数据. 2.SelectedCells[N].Value的属性可以获取某一行数据中某列的数据,其 ...

  10. X-Plane飞行模拟器购买安装

    要玩起X-Plane第一个步骤当然是购买了,要购买其实非常简单,只需要一张能够支持MasterCard或者其他外币结算的信用卡,在http://www.x-plane.com/官网上购买即可,比逛淘宝 ...