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. Spring Boot 使用IntelliJ IDEA创建一个web开发实例(五)

    使用application.ym进行多环境配置 1.配置激活选项 spring: profiles: active: dev 2.在配置文件添加若干个英文状态下的短横线即可区分 spring: pro ...

  2. python学习笔记5--random

    一.random模块 import random,string print(random.randint(1,199))#1-199随机取一个整数 print(string.digits) #所有的数 ...

  3. sublime text3 最常用的快捷键及插件

    A:最常用的快捷键 Tab:自动补齐代码 <!--div+Tab 其它标签一样--><div></div> emmet常用的使用方法 <!--ul>li ...

  4. 自己写的一个小的剪刀——石头——布游戏的GUI程序

    很简单的一个程序,建议各位初学Java的同学可以试试写写这个程序: import javax.swing.JOptionPane; public class Game { public static ...

  5. 使iis支持asp.net扩展

    打开控制面板 - 程序和功能,点击左边 “打开或关闭 Windows 功能”. 在弹出的对话框中,展开 “Internet信息服务”,展开“万维网服务”,展开“应用程序开发功能”,勾选“ASP”和“A ...

  6. 不忘初心,方得始终——NOIP2016前的感悟

    不忘初心,方得始终 袛园精舍钟声响,奏诸世事本无常.沙罗双树失花色,盛者转衰如沧桑.骄者难久,恰如春宵一梦.猛者遂灭,好似风前之尘.    ——题记   人生中最令人恐惧的恐怕就是选择了,现在的你拥有 ...

  7. dubbox ExceptionMapper Filter request response 数据获取 数据传递

    dubbx虽然是基于jboss的resteasy实现restfull,但是对resteasy原生的配置却不支持(可能是考虑到dubbo本事的设计模式及实现难度,但是和大部分framework的设计风格 ...

  8. win10+anaconda虚拟环境python3.6+cuda9.0+cudnn7+pytorch0.4.1

    anaconda下jupyter notebook的打开目录的修改:参考:https://www.cnblogs.com/amberdata/p/7907845.html pytorch官网:http ...

  9. Html5使用history对象history.pushState()和history.replaceState()方法添加和修改浏览历史记录

    根据网上参考自己做个笔记:参考网址:http://javascript.ruanyifeng.com/bom/history.html history.pushState() HTML5为histor ...

  10. 利用正则表达式去除所有html标签,只保留文字

    后台将富文本编辑器中的内容返回到前端时如果带上了标签,这时就可以利用这种方法只保留文字. 标签的格式有以下几种 1.<div class="test"></div ...