P76

参考:http://www.cnblogs.com/lanxuezaipiao/p/4132096.html

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

简介

智能指针是存储指向动态分类(堆)对象的指针的类,用于生存期控制,确保在离开指针作用域时,自动正确销毁动态分配的对象。

通过引用计数的技术来实现,每使用它一次,内部的引用计数就加1,每析构一次,引用计数减1,减到0时就销毁对象,回收内存。

头文件 #include <memory>

分类

三种智能指针:

  • std::shared_ptr  实现共享式拥有,多个指针可以指向相同的对象,该对象和相关资源会在最后一个reference被销毁时释放
  • std::unique_ptr  实现独占式拥有,保证同一时间内只有一个指针可以指向该对象
  • std::weak_ptr    持有被shared_ptr所管理对象的引用,但是不会改变引用计数值。允许共享但不拥有某对象

另一方面,auto_ptr已经被废弃,C98,之前和std::unique_ptr一个意思

使用

 #include <iostream>
#include <string>
#include <memory> class report
{
private:
std::string str;
public:
report(const std::string s) : str(s) {
std::cout << "Object created.\n";
}
~report() {
std::cout << "Object deleted.\n";
}
void comment() const {
std::cout << str << "\n";
}
}; int main() {
{
std::auto_ptr<report> ps(new report("using auto ptr"));
ps->comment();
} {
std::shared_ptr<report> ps(new report("using shared ptr"));
ps->comment();
} {
std::unique_ptr<report> ps(new report("using unique ptr"));
ps->comment();
}
return ;
}

注意:智能指针的初始化必须使用复制初始化而不能采用赋值初始化,因为智能指针类的构造函数是explicit,只能够显示的调用构造函数

str_ptr<string> p1 = new string("hello")    //error,赋值初始化隐式调用构造函数

str_ptr<string> p2(new string("world"))    //OK

unique_ptr注意拥有权必须用move

    unique_ptr<string> p2(new string("hello"));
unique_ptr<string> p3;
p3 = std::move(p2);

p2失去拥有权不能在被调用了

unique_ptr比auto_ptr的好处在于如果p3 = p2,unique_ptr在编译时就能发现错误,auto_ptr要在运行时才能发现错误

shared_ptr实现

参考:http://www.jianshu.com/p/0300b2d833af

分析:

成员,一个模板指针T *p,一个引用计数的int *count;

成员函数:

构造函数:接受T *参数初始化成员p,初始化count为1,注意count需要new,p就不用了,p是在使用过程中new的

复制构造函数:注意形参不要用const类型,是引用类型的smart_point类,让count等于形参.count++,让p等于形参.p

析构函数:首先先对*count自减,不是0就算了,是0的话delete   p和count

然后写重载几个运算符*,->和=

*是返回T&类型,返回*p

->返回T*类型也就是指针,返回的是p

=首先将*count加一,然后为了防止自己=自己要判断count自减后是不是0,最后用对count和p进行赋值操作

 #include <string>
#include <iostream>
using namespace std; template <typename T>
class smart_ptrs { public:
smart_ptrs(T*); //用普通指针初始化智能指针
smart_ptrs(smart_ptrs&); T* operator->(); //自定义指针运算符
T& operator*(); //自定义解引用运算符
smart_ptrs& operator=(smart_ptrs&); //自定义赋值运算符 ~smart_ptrs(); //自定义析构函数 private:
int *count; //引用计数
T *p; //智能指针底层保管的指针
}; template <typename T>
smart_ptrs<T>::smart_ptrs(T *p) : count(new int()), p(p) {
cout << "创建对象" << *p << ",引用计数:" << *count << endl;
} template <typename T>
//对普通指针进行拷贝,同时引用计数器加1,因为需要对参数进行修改,所以没有将参数声明为const
smart_ptrs<T>::smart_ptrs(smart_ptrs &sp) : count(&(++*sp.count)), p(sp.p) {
cout << "调用复制构造函数,拷贝:" << *sp.p << ",引用计数:" << *count << endl;
} template <typename T>
T* smart_ptrs<T>::operator->() {
return p;
} template <typename T>
T& smart_ptrs<T>::operator*() {
return *p;
} template <typename T>
smart_ptrs<T>& smart_ptrs<T>::operator=(smart_ptrs& sp) {
++*sp.count;
if (--*count == ) { //自我赋值同样能保持正确
delete count;
delete p;
}
this->p = sp.p;
this->count = sp.count;
cout << "赋值操作," << *this->p << "引用计数变为" << *this->count << endl;
return *this;
} template <typename T>
smart_ptrs<T>::~smart_ptrs() {
if (--*count == ) {
delete count;
delete p;
}
} int main()
{
smart_ptrs<string> pstr(new string("abc"));
smart_ptrs<string> pstr2(pstr);
smart_ptrs<string> pstr3(new string("bcd"));
pstr3 = pstr2; system("pause");
}

Smart Pointer 智能指针的更多相关文章

  1. C++2.0新特性(六)——<Smart Pointer(智能指针)之shared_ptr>

    Smart Pointer(智能指针)指的是一类指针,并不是单一某一个指针,它能知道自己被引用的个数以至于在最后一个引用消失时销毁它指向的对象,本文主要介绍C++2.0提供的新东西 一.Smart P ...

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

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

  3. C++ smart pointer智能指针

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

  4. Smart pointer 智能指针小总结

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

  5. C++2.0新特性(八)——<Smart Pointer(智能指针)之unique_ptr>

    一.概念介绍 unique_ptr它是一种在异常发生时可帮助避免资源泄露的smart pointer,实现了独占式拥有的概念,意味着它可确保一个对象和其他相应资源在同一时间只被一个pointer拥有, ...

  6. C++2.0新特性(七)——<Smart Pointer(智能指针)之weak_ptr>

    一.weak_ptr出现的意义 上一节提到过shared_ptr,它会自动释放“不再需要使用的对象”的相应的资源,但是它不是万能的,在某些时候(比如说循环引用),它会显得力不从心,这就是weak_pt ...

  7. C++11特性 - Smart Pointers 智能指针

    已经有成千上万的文章讨论这个问题了,所以我只想说:现在能使用的,带引用计数,并且能自动释放内存的智能指针包括以下几种:         unique_ptr: 如果内存资源的所有权不需要共享,就应当使 ...

  8. 智能指针 shared_ptr 解析

    近期正在进行<Effective C++>的第二遍阅读,书里面多个条款涉及到了shared_ptr智能指针,介绍的太分散,学习起来麻烦.写篇blog整理一下. LinJM   @HQU s ...

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

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

随机推荐

  1. 转:iOS7导航栏遮盖问题的解决

    在开发iOS7的界面的时候,有时候你会发现UIViewController里的subView有时会被导航栏遮盖,原因是iOS7鼓励全屏布局,UIViewController中的self.view的坐标 ...

  2. bzoj千题计划126:bzoj1038: [ZJOI2008]瞭望塔

    http://www.lydsy.com/JudgeOnline/problem.php?id=1038 本题可以使用三分法 将点按横坐标排好序后 对于任意相邻两个点连成的线段,瞭望塔的高度 是单峰函 ...

  3. java 数字转换成字符串

    一.各种数字类型转换成字符串型:  public static void main(String[] args) { double value = 123456.123; String str = S ...

  4. MYSQL 在当前时间加上或减去一个时间段

    update user set time1=now(),time2=date_add(NOW(), interval 1 MONTH) where id=1; date_add() 增加date_su ...

  5. 四、Kafka 核心源码剖析

    一.Kafka消费者源码介绍 1.分区消费模式源码介绍 分区消费模式直接由客户端(任何高级语言编写)使用Kafka提供的协议向服务器发送RPC请求获取数据,服务器接受到客户端的RPC请求后,将数据构造 ...

  6. [php]php总结(2)

    18.数组$arr[null] = 20; //array([]=>20)$arr[] = 20;//默认为下一个下标赋值unset()可以删除数组元素,但不会重建索引array_values( ...

  7. HDU 1427 速算24点 (深搜)

    题目链接 Problem Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A(1),2,3,4,5,6,7,8,9,10,J(11),Q(12),K(13).要求只用' ...

  8. PHP autoload自动加载机制

    原文地址: http://www.jb51.net/article/31399.htm 一直不是很明白__autoload()和spl_autoload_register()到底有什么不同,找到了一个 ...

  9. OpenCV LIBTIFF_4.0 link errors

    以前用Caffe用的好好的,今天重装后居然报了很多这样的错误 /usr/lib/libopencv_highgui.so.' 1> /usr/lib/libopencv_highgui.so.' ...

  10. sleep命令

    sleep支持睡眠(分,小时) sleep 1 睡眠1秒 sleep 1s 睡眠1秒 sleep 1m 睡眠1分 sleep 1h 睡眠1小时