对于tr1::shared_ptr在安装vs同一时候会自带安装,可是版本号较低的不存在。而boost作为tr1的实现品,包括

“Algorithms

Broken Compiler Workarounds

Concurrent Programming

Containers

Correctness and Testing

Data Structures

Domain Specific

Function Objects and Higher-order Programming

Generic Programming

Image Processing

Input/Output

Inter-language Support

Iterators

Language Features Emulation

Math and Numerics

Memory

Parsing

Patterns and Idioms

Preprocessor Metaprogramming

Programming Interfaces

State Machines

String and Text Processing

System

Template Metaprogramming

Miscellaneous ”

等一系列更安全、更丰富的c++函数或库的实现品。能够在官网下载,当中包括具体的API文档与例程。当中boost::shared_ptr是对tr1的shared_ptr的实现。作为智能指针(类指针对象)二者的用法相似。具体见下方代码。

头文件

#include <iostream>

using namespace std;

class Test
{
public:
Test();
~Test();
};

实现文件

#include "head.h"

Test::Test()
{
cout << "construct Test." << endl;
} Test::~Test()
{
cout << "destruct Test." << endl;
}

main文件

//boost中shared_ptr头文件
#include <boost/shared_ptr.hpp> //vs2005一并中安装的库是不带shared_ptr的
#include <memory>
#include "head.h" //实验tr1::shared_ptr
void test_shared_ptr()
{
cout << "I am tr1." << endl;
tr1::shared_ptr<Test> p_test(new Test());
} //实验boost/shared_ptr
void test_boost_shared_ptr()
{
cout << "I am boost." << endl;
boost::shared_ptr<Test> p_boost_test(new Test());
} int main()
{
test_shared_ptr();
cout << "**************************" << endl;
test_boost_shared_ptr(); system("pause");
}

运行效果:

上述两种方式是使用对象管理资源的最佳实现。标准库也提供了auto_ptr,可是这样的对象不可以复制。

对于使用对象管理资源的思路,C++t推荐的原则是RAII,即Resource aquisition is initialasition。

***************************************************************************

原创,本文链接:http://blog.csdn.net/u012150179/article/details/37965931

智能指针tr1::shared_ptr、boost::shared_ptr使用的更多相关文章

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

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

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

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

  3. C++11智能指针(unique_ptr、shared_ptr、weak_ptr)(转)

    原文地址:https://blog.csdn.net/king_way/article/details/95536938

  4. C++ auto_ptr智能指针的用法

    C++中指针申请和释放内存通常采用的方式是new和delete.然而标准C++中还有一个强大的模版类就是auto_ptr,它可以在你不用的时候自动帮你释放内存.下面简单说一下用法. 用法一: std: ...

  5. 智能指针剖析(下)boost::shared_ptr&其他

    1. boost::shared_ptr 前面我已经讲解了两个比较简单的智能指针,它们都有各自的优缺点.由于 boost::scoped_ptr 独享所有权,当我们真真需要复制智能指针时,需求便满足不 ...

  6. C++智能指针剖析(下)boost::shared_ptr&其他

    1. boost::shared_ptr 前面我已经讲解了两个比较简单的智能指针,它们都有各自的优缺点.由于 boost::scoped_ptr 独享所有权,当我们真真需要复制智能指针时,需求便满足不 ...

  7. 智能指针 shared_ptr 解析

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

  8. 智能指针auto_ptr & shared_ptr

    转载:智能指针auto_ptr 很多人听说过标准auto_ptr智能指针机制,但并不是每个人都天天使用它.这真是个遗憾,因为auto_ptr优雅地解决了C++设计和编码中常见的问题,正确地使用它可以生 ...

  9. 智能指针shared_ptr新特性shared_from_this及weak_ptr

    enable_shared_from_this是一个模板类,定义于头文件<memory>,其原型为: template< class T > class enable_shar ...

随机推荐

  1. SAP MM 模块常用的数据表

    SAP MM模块常用数据表: Table Table Class Application Class Data Class Description EBAN TRANSP  ME Transactio ...

  2. Screen多视窗远程控制管理服务

    Screen是一款由GNU开源计划开发的多视窗远程控制管理服务,简单来说就是为了解决上述情况中网络异常中断或同时控制多个远程窗口而设计的程序. Screen服务程序不仅能够解决上述问题,而且用户在使用 ...

  3. http://www.linuxidc.com/Linux/2016-04/129738.htm

    http://www.linuxidc.com/Linux/2016-04/129738.htm

  4. hdoj-1301-Jungle Roads【最小生成树】

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  5. Spring框架学习(7)spring mvc入门

    内容源自:spring mvc入门 一.spring mvc和spring的关系 spring mvc是spring框架提供的七层体系架构中的一个层,是spring框架的一部分,是spring用于处理 ...

  6. 【转】dependency injection 的例子

    Dependency Injection in PHP. Create your own DI container. / blog / PHP By my opinion one of the big ...

  7. OC中数组的使用方法

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {     // 创建数组     NS ...

  8. HTML+JavaScript实现链式运动特效

    在学习js的过程中,发现这家伙做特效真是不错,尽管说眼下水平还不够,只是也能写点简单的效果. 今天分享一个简单的运动框架.然后利用这个框架实现简单的链式运动特效. 1.move.js 在运动框架中.主 ...

  9. CCPlatformConfig(设置执行平台 iOS android win32等。。。)

    #ifndef __CC_PLATFORM_CONFIG_H__ #define __CC_PLATFORM_CONFIG_H__ /** Config of cocos2d-x project, p ...

  10. PHP变量引用赋值与变量赋值变量的区别

    变量默认总是传值赋值.那也就是说,当将一个表达式的值赋予一个变量时,整个原始表达式的值被赋值到目标变量.这意味着,例如,当一个变量的值赋予另外一个变量时,改变其中一个变量的值,将不会影响到另外一个变量 ...