boost准模板库scoped_ptr指针的使用以及auto_ptr智能指针的对照

首先我们看看scoped_ptr的基本使用,包括了swap(),get(),reset()的使用,重要的提醒是作用域结束的时候会自己主动析构,无需手动的释放资源:

#include<boost/smart_ptr.hpp>
#include<iostream>
using namespace std;
using namespace boost;
struct posix_file
{
	posix_file(const char * file_name)//一个文件类
	{
		cout<<"open file"<<file_name<<endl;//构造函数模拟打开文件
	}
	~posix_file()//析构函数模拟关闭文件
	{
		cout<<"close file"<<endl;
	}
};
int main()
{
	scoped_ptr<int> p(new int);//一个指向int变量的指针的scoped_ptr
	if(p)//在bool语境中測试指针是否有效
	{
		*p=10;//像一般指针一样使用解指针操作符*
		cout<<*p<<endl;
	}
	scoped_ptr<int> q(new int);//创建新的int指针的scoped_ptr
	*q=20;
	p.swap(q);//交换两个scoped_ptr指向的对象
	cout<<"p value:"<<*p<<endl<<"q value:"<<*q<<endl;
	p.reset();//置空scoped_ptr

	if(!p)
	{
		cout<<"scoped==null"<<endl;
		scoped_ptr<posix_file> fp(new posix_file("/temp/a.txt"));//局部scoped_ptr在作用域结束的时候自己析构
	}
	getchar();
}

执行结果例如以下:

接下来,让我们看一看auto_ptr和scoped_ptr之间的联系和差别,同一时候,学者使用两者之间的指针控制权力转移

代码示比例如以下:

#include<boost/scoped_ptr.hpp>
#include<iostream>
using namespace std;
using namespace boost;
int main()
{
	auto_ptr<int> auto_p(new int(10));//一个int自己主动指针
	cout<<"auto_p value:"<<*auto_p<<endl;
	scoped_ptr<int> sp(auto_p);//从自己主动指针auto_ptr获得原始指针
	if(auto_p.get()==0)//原auto_ptr不再拥有指针
		cout<<"auto_p不再拥有指针"<<endl;
	cout<<"sp value:"<<*sp<<endl;
	auto_p.reset(new int(20));//auto_ptr获得新指针
	cout<<"auto_p<-->sp:"<<*auto_p<<"<-->"<<*sp<<endl;
	auto_ptr<int> auto_p2;
	auto_p2=auto_p;//auto_p2获得auto_p的原始指针,auto_p失去原始指针
	if(auto_p.get()==0)
	{
		cout<<"auto_p失去指针!"<<endl;
	}
	cout<<"auto_p2 value:"<<*auto_p2<<endl;
	//auto_ptr<int> auto_p3(sp);//auto_ptr无法取得scoped_ptr的指针
	//cout<<"auto_p3:"<<*auto_p3<<endl;
	//if(auto_p2.get()==0)//
	//{
	//	cout<<"auto_ptr的auto_p2失去指针"<<endl;
	//}//
	//cout<<""<<*sp2<<endl;
	//sp2=sp;编译出错,无法相互赋值
	getchar();
}

以下是执行结果,能够看出auto_ptr和auto_ptr能够相互移交指针,可是scoped_ptr能够从auto_ptr获得指针而不能反向从scoped_tr获得指针

				
时间: 2024-08-26 11:04:53

boost准模板库scoped_ptr指针的使用以及auto_ptr智能指针的对照的相关文章

第二讲 auto_ptr智能指针

// STL.cpp : 定义控制台应用程序的入口点. // //智能指针在其生命周期结束后会自动调用delete #include "stdafx.h" #include<iostream> #include<memory> using namespace std; class WebSite { public: WebSite(int x){i = x;cout << i << "调用构造函数" <<

shared_ptr 和auto_ptr智能指针

shared_ptr:计数的智能指针 它是一个包装了new操作符在堆上分配的动态对象,但它实现的是引用计数型的智能指针 ,可以被自由地拷贝和赋值,在任意的地方共享它,当没有代码使用(引用计数为0)它时才删除被包装的动态分配的对象.shared_ptr也可以安全地放到标准容器中,并弥补了auto_ptr因为转移语义而不能把指针作为STL容器元素的缺陷. 线程安全性: shared_ptr 本身不是 100% 线程安全的.它的引用计数本身是安全且无锁的,但对象的读写则不是,因为 shared_ptr

Qt 智能指针学习(7种QT智能指针和4种std智能指针)

从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include <QApplication> #include <QLabel> int main(int argc, char *argv[]) { QApplication app(argc, argv); QLabel *label = new QLabel("Hello Dbzhang800!"); label->show(); return app.exec(); } 在  从 Qt

Part6 数组、指针与字符串 6.10 智能指针 6.11 vector对象

6.10 智能指针C++11 提供智能指针的数据类型,对垃圾回收技术提供了一些支持,实现一定程度的内存管理 unique_ptr:不允许多个指针共享资源,可以用标准库中的move函数转移指针shared_ptr:多个指针共享资源weak_ptr:可复制shared_ptr,但其构造或者释放对资源不产生影响 6.11 vector对象为什么需要vector? 封装任何类型的动态数组,自动创建和删除. 数组下标越界检查. 例6-18 中封装的ArrayOfPoints也提供了类似功能,但只适用于一种

auto_ptr 智能指针(C98)

#include<iostream> using namespace std; void main(){ //auto_ptr for ( int i = 0; i < 100000; i++) { double *p = new double; //智能指针自动完成内存释放 把堆当作栈来处理 //相当于delete p auto_ptr< double> a_p(p); } cin.get(); } 查看任务管理器:

为什么auto_ptr智能指针不能作为STL标准容器的元素

为什么auto_ptr不可以作为STL标准容器的元素 假如有这样的一段代码,是否能够运行? ? 1 2 3 4 5 6 7 8 9 int costa_foo() {     vector< auto_ptr<int> > v(10);     int i=0;     for(;i<10;i++)     {           v[i]=auto_ptr<int>(new int(i));     }   } 答案是否定的,甚至这段代码是无法编译通过的. 错误

C++中auto_ptr智能指针

C++中的auto_ptr(俗称智能指针)所做的事情,使用起来就像普通指针,但当其动态分配内存时,不再需要考虑清理问题.当它的生存期结束时,系统会自动清理它指向的内存. 其实auto_ptr是一个模版类(注意实质上还是一个类).主要解决内存泄漏问题. 原理:其实就是RAII,在构造的时候获取资源,在析构的时候释放资源,并进行相关指针操作的重载,使用起来就像普通的指针(!!!其实实质上还是一个类). 0. 先看看如何使用: int * a=new int(5); auto_ptr<int> ap

函数指针、类型别名与智能指针

1 // run in windows: std::system("pause") 2 #include <cstdlib> 3 #include <iostream> 4 #include <memory> 5 #include <functional> 6 7 using namespace std; 8 using namespace std::placeholders; 9 10 template <typename T&g

第61课 智能指针类模板

1. 智能指针的意义 (1)现代C++开发库中最重要的类模板之一 (2)C++中自动内存管理的主要手段 (3)能够在很大程度上避开内存相关的问题(如内存泄漏.内存的多次释放等) 2. STL中的智能指针 (1)auto_ptr智能指针 ①生命周期结束时,销毁指向的内存空间 ②只能用来管理单个动态创建的对象,而不能管理动态创建的数组.即不能指向堆数组,只能指针堆对象(变量) int* pn = new int[100]; auto_ptr<int> ap(pn); //auto_ptr指向堆数组