转载http://www.cnblogs.com/kadinzhu/archive/2011/12/12/2284826.html

看《effective c++》,作者一直强调用std::tr1::shared_ptr,比起auto_ptr好多了。

shared_ptr采用引用计数,多个指针可以指向同一个对象;auto_ptr就不能,只能运行一个指针指向一个对象:如果要指针赋值,那么原来的指针要放弃对该对象的所有权。

恩,以后都用shared_ptr。

shared_ptr在最新的c++11中,已经被列入了标准指针,而auto_ptr则出局了。

说了那么多,shared_ptr采用RAII技术,是防止内存泄露的神器。

按bnu_chenshuo的说法,他最后一次看见代码中的内存泄露还是04年他做实习生的时候。

而C++沉思录的作者AndrewKoenig也极力推荐使用标准库,不用指针。

看下面的程序,我new了一个对象,并没有在程序中使用delete,但是,运行程序,其构造函数仍然运行!这就是shared_ptr,如果要预防内存泄露,它就是最佳选择!

# include <iostream>
# include <tr1/memory>
using namespace std;
class A {
public:
A() {
cout << "construct A!!!" << endl;
}
;
~A() {
cout << "destruct A!!!" << endl;
}
;
};
class B: public A {
public:
B() {
cout << "construct B!!!" << endl;
}
;
~B() {
cout << "destruct B!!!" << endl;
}
;
};
int main() {
// B* ptrB0 = new B();
std::tr1::shared_ptr<B> ptrB1(new B);
}

运行结果:

construct A!!!

construct B!!!

destruct B!!!

destruct A!!!

c++智能指针《二》 std::tr1::shared_ptr的更多相关文章

  1. C++ std::tr1::shared_ptr使用说明

    1. 介绍 shared_ptr 是通过指针保持某个对象的共享拥有权的智能指针. 若干个 shared_ptr 对象能够拥有同一个对象:最后一个指向该对象的 shared_ptr 被销毁或重置时.该对 ...

  2. C++11智能指针之std::unique_ptr

    C++11智能指针之std::unique_ptr   uniqut_ptr是一种对资源具有排他性拥有权的智能指针,即一个对象资源只能同时被一个unique_ptr指向. 一.初始化方式 通过new云 ...

  3. 智能指针(二):shared_ptr实现原理

    前面讲到auto_ptr有个很大的缺陷就是所有权的转移,就是一个对象的内存块只能被一个智能指针对象所拥有.但我们有些时候希望共用那个内存块.于是C++ 11标准中有了shared_ptr这样的智能指针 ...

  4. 深入学习c++--智能指针(二) weak_ptr(打破shared_ptr循环引用)

    1. 几种智能指针 1. auto_ptr: c++11中推荐不使用他(放弃) 2. shared_ptr: 拥有共享对象所有权语义的智能指针 3. unique_ptr: 拥有独有对象所有权语义的智 ...

  5. C++ 智能指针二

    /* 智能指针shared_ptr注意点 */ #include <iostream> #include <string> #include <memory> // ...

  6. c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)

    一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...

  7. 【校招面试 之 C/C++】第26题 C++ 智能指针(二)之 share_ptr

    1.综述 shared_ptr 是一个标准的共享所有权的智能指针, 允许多个指针指向同一个对象. 定义在 memory 文件中(非memory.h), 命名空间为 std. shared_ptr 是为 ...

  8. c++智能指针的使用,shared_ptr,unique_ptr,weak_ptr

    c++智能指针的使用 官方参考 普通指针的烦恼:内存泄漏,多次释放,提前释放 智能指针 负责自动释放所指向的对象. 三种智能指针 shared_ptr,unique_ptr,weak_ptr: 将sh ...

  9. 智能指针(1)-std::unique_ptr

    std::unique_ptr std::unique_ptr是一种几乎和原始指针一样高效的智能指针,对所管理的指针资源拥有独占权.由C++11标准引入,用于替代C++98中过时的std::auto_ ...

随机推荐

  1. c语言typedef与define的相同

    #include <stdio.h> #include <stdlib.h> #define INT int typedef short SHORT;//看此处有没有分号 // ...

  2. JAVA并发实现五(生产者和消费者模式wait和notify方式实现)

    package com.subject01; import java.util.PriorityQueue; /** * 通过wait和notify 实现 * 生产者-消费者模型:当队列满时,生产者需 ...

  3. JAVA并发实现二(线程中止)

    package com.subject01; public class InterruptDemo { public static void main(String[] args) { SimpleT ...

  4. pyqt托盘例子

    # -*- coding: cp936 -*- #!/usr/bin/env python # -*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui ...

  5. wpf新增记录时用多线程的问题

    多线程虽然可以增加用户操作体验,但是有时候会出现意想不到的错误. 如果采用分布式,数据库在另外服务器上,当网络出现问题,或者数据库繁忙,那么新增数据就会等待,这时候用户如果以为没有操作,而多次点击新增 ...

  6. 验证docker的Redis镜像也存在未授权访问漏洞

    看到了这篇老外的博客:Over 30% of Official Images in Docker Hub Contain High Priority Security Vulnerabilities于 ...

  7. ORA-01652:无法通过128(在表空间TEMP中)扩展temp段

    在Oracle数据库中进行order by or group by.索引的创建和重创建.distinct操作.union & intersect & minus sort-merge ...

  8. CSS样式之背景、文本

    一.背景     1.背景颜色用background-color属性,例如:body{background-color:red}     2.用图像做背景用background-image属性,例如b ...

  9. Linux设置日期

    $ date -s "2016-07-13 14:54" 把时间设置为2016-07-13 14:54

  10. Silverlight Visifire控件 .net后台控制aspx页面控件的显示与隐藏,动态给控件赋值,选定默认值的设定

    .net后台代码: 控件的显示与隐藏: this.dateStart.Visibility = Visibility.Collapsed;//不显示控件 this.dateYear.Visibilit ...