auto_ptr是这样一种指针,它是“它所指向的对象”的拥有者,所以,当身为对象拥有者的auto_ptr被摧毁时,该对象也将遭到摧毁。auto_ptr要求一个对象只能有一个拥有者,严禁一物二主。
设计动机:
1获取一些资源
2执行一些动作
3释放所获取的资源,不用自己调用delete,不用担心异常。。。的原因,导致内存泄漏的问题了。
#include<iostream>
#include<memory>
#include<string>
using namespace std;
int main(){
auto_ptr<int>p(new int(8));
cout<<*p<<endl;
auto_ptr<string>pp(new string);
*pp="hello";
cout<<*pp<<endl;
auto_ptr<double>ppp(new double(1.23));
cout<<*ppp<<endl;
return 0;
}
#include<iostream>
#include<string>
#include<memory>
using namespace std;
int main(){
char *p="hello";
cout<<p<<endl;
int *pp=new int(100);
cout<<*pp<<endl;
char *ppp=new char('a');
cout<<*ppp<<endl;
char *ap=new char;
//string s1="hello";
//ap=&s1[0];
ap="hello";
cout<<ap<<endl;
return 0;
}
#include<iostream>
#include<string>
#include<memory>
using namespace std;
int main(){
auto_ptr<int>ap1(new int(100));
cout<<*ap1<<endl;
auto_ptr<int>ap2(new int);
*ap2=10086;
cout<<*ap2<<endl;
ap1=ap2;
cout<<*ap1<<endl;//输出10086
cout<<*ap2<<endl;//输出10086
return 0;
}
#include<iostream>
#include<memory>
#include<string>
using namespace std;
int main(){
auto_ptr<int>ap1(new int(10086));
auto_ptr<int>ap2(ap1);
cout<<*ap1<<endl;//转移拥有权,因此此句报错,注意在VC6.0是不报错的,VC对STL支持不那么好
cout<<*ap2<<endl;
auto_ptr<int>ap3(new int(10087));
auto_ptr<int>ap4;
ap4=ap3;
cout<<*ap3<<endl;//转移拥有权,因此此句报错,VS2008报错,VC6.0能运行,建议使用VS
cout<<*ap4<<endl;
//auto_ptr<int>ap5;
//ap5=new int(10088); //auto_ptr指针不能用普通指针初始化
//cout<<*ap5<<endl;
return 0;
}
#include<iostream>
#include<string>
#include<memory>
using namespace std;
void show(auto_ptr<int>p){
cout<<*p<<endl;
}
int main(){
auto_ptr<int>p(new int(10086));
show(p);
//cout<<*p<<endl;已经转移拥有权了
system("pause");
return 0;
}
const auto_ptr<int>p(new int(10086));
auto_ptr<int>pp(p); //这是错误的,因为const auto_ptr<type>x的拥有权不能能被更改
#include<iostream>
#include<memory>
using namespace std;
template<typename T>
ostream& operator<<(ostream& out,const auto_ptr<T>& p){
if(p.get()==NULL){
out<<"NULL";
}else{
out<<*p;
}
return out;
}
int main(){
auto_ptr<int>p(new int(42));
auto_ptr<int>q;
cout<<"after initialization:"<<endl;
cout<<"P:"<<p<<endl;
cout<<"Q:"<<q<<endl;
q=p;
cout<<"P:"<<p<<endl;
cout<<"Q:"<<q<<endl;
*q+=13;
cout<<"after change and reassignment:"<<endl;
cout<<"P:"<<p<<endl;
cout<<"Q:"<<q<<endl;
system("pause");
return 0;
}
#include<iostream>
#include<memory>
using namespace std;
template<typename T>
ostream& operator<<(ostream& out,const auto_ptr<T>& p){
if(p.get()==NULL){
out<<"NULL";
}else{
out<<*p;
}
return out;
}
int main(){
const auto_ptr<int>p(new int(42));
const auto_ptr<int>q(new int(0));
const auto_ptr<int>r;
cout<<"after initialization:"<<endl;
cout<<"p:"<<p<<endl;
cout<<"q:"<<q<<endl;
cout<<"r:"<<r<<endl;
*q=*p;
*p=-77;
cout<<"after assigning values"<<endl;
cout<<"p:"<<*p<<endl;
cout<<"q:"<<*q<<endl;
cout<<"r:"<<r<<endl;
system("pause");
return 0;
}
const int *p不可以改变他的指针,但是可以改变指针对应的值 如*p=100;
auto_ptr源码如下
namespace std{
template<typename Y>
struct auto_ptr_ref{};
template<typename T>
class auto_ptr{
public:
typedef T element_type; //类型
explicit auto_ptr(T* ptr=0) throw();//显示调用构造函数,不抛出任何异常
auto_ptr(auto_ptr&)throw(); //拷贝构造函数
template<typename U>
auto_ptr(auto_ptr<U>&)throw(); //拷贝构造函数
auto_ptr& operator=(auto_ptr&)throw(); //=操作符重载
template<typename U>
auto_ptr& operator=(auto_ptr<U>&)throw(); //=操作符重载
~auto_ptr()throw(); // 析构函数
T* get() const throw(); //获取元素指针
T& operator*() const throw(); // 解析,获得元素
T* operator->()const throw(); // 获得元素
T* release() throw(); //释放
void reset(T* ptr=0)throw(); //重置指针为0
public:
auto_ptr(auto_ptr_ref<T> rhs)throw();
auto_ptr& operator=(auto_ptr_ref<T> rhs)throw();
template<typename U>
operator auto_ptr_ref<U>() throw();
template<typename U>
operator auto_ptr<U>() throw();
};
}
分享到:
相关推荐
动态分配的资源,交给⼀个类对 象去管理,当类对象声明周期结束时,⾃动调⽤析构函数释放资源 常⽤的智能指针 (1) shared_ptr 实现原理:采⽤引⽤计数器的⽅法,允许多个智能指针指向同⼀个对象,每当多⼀个指针指向...
在Delphi2009中实现类似于STL中auto_ptr的智能指针!关于此智能指针的原理及解释请进入以下网址: http://www.cnblogs.com/felixYeou/archive/2008/09/06/1285806.html
类似于STL中auto_ptr的文件指针
2、std::auto_ptr std::auto_ptr 属于 STL,当然在 namespace std 中,包含头⽂件 #include<memory> 便可以使⽤。std::auto_ptr 能够⽅便的管理单个堆 内存对象。 我们从代码开始分析: void TestAutoPtr() { std::...
现代的C/C++类库一般会提供智能指针来作为内存管理的折中方案,比如STL的auto_ptr,Boost的Smart_ptr库,QT的QPointer家族,甚至是基于C语言构建的GTK+也通过引用计数来实现类似的功能。Linux内核是如何解决这个问题...
智能指针的各种实现方法,从stl和boost等角度上说明
Smart Pointer(智能指针) shared_ptr weak_ptr unique_ptr auto_ptr Type Trait和Type Utility Type Trait Reference Warpper(外覆器) Function Type Warpper(外覆器) 辅助函数 std::min/std::max std::swap 增补的...
条款8:永不建立auto_ptr的容器 条款9:在删除选项中仔细选择 条款10:注意分配器的协定和约束 条款11:理解自定义分配器的正确用法 条款12:对STL容器线程安全性的期待现实一些 vector和string 条款13:尽量...
条款8: 千万不要把auto_ptr放入容器中 条款9: 小心选择删除选项 条款10: 当心allocator的协定和约束 条款11: 了解自定义allocator的正统使用法 条款12: 对STL容器的线程安全性的期待现实一些 vector和string ...
条款8: 千万不要把auto_ptr放入容器中 条款9: 小心选择删除选项 条款10: 当心allocator的协定和约束 条款11: 了解自定义allocator的正统使用法 条款12: 对STL容器的线程安全性的期待现实一些
条款8: 千万不要把auto_ptr放入容器中 条款9: 小心选择删除选项 条款10: 当心allocator的协定和约束 条款11: 了解自定义allocator的正统使用法 条款12: 对STL容器的线程安全性的期待现实一些 vector和string ...
记得在销毁容器前delete那些指针 条款8:永不建立auto_ptr的容器 条款9:在删除选项中仔细选择 条款10:注意分配器的协定和约束 条款11:理解自定义分配器的正确用法 条款12:对STL容器线程安全性的...
而每一本C++的经典著作所提供的方案是使用智能指针(STL的标准类auto_ptr)。 在Object Pascal中,这个问题变得非常的简单,程序员不必为此大费周折。如果Object Pascal的类在构造函数中抛出异常,编译器会自动调用...
38.2 auto_ptr 38.3 pair类 38.4 本地化 38.5 其他有趣的类 第五部分 C++应用程序范例 第39章 集成新的类:自定义字符串类 39.1 StrType类 39.2 构造函数和析构函数 39.3 字符串I/O 39.4 赋值函数 39.5 连接 39.6 ...
13.15 auto_ptr类与动态内存分配 13.16 标准库异常层次 小结 术语 自测练习 自测练习答案 练习 第14章 文件处理 14.1 简介 14.2 数据的层次 14.3 文件和流 14.4 建立顺序访问文件 14.5 读取顺序访问文件中的...
13.15 auto_ptr类与动态内存分配 13.16 标准库异常层次 小结 术语 自测练习 自测练习答案 练习 第14章 文件处理 14.1 简介 14.2 数据的层次 14.3 文件和流 14.4 建立顺序访问文件 14.5 读取顺序访问文件中的...
2.3.8 关于auto_ptr 11 2.4 一个简单的C++垃圾回收器 11 2.5 详细讨论GCPtr 23 2.5.1 GCPtr的数据成员 23 2.5.2 函数findPtrInfo() 24 2.5.3 GCIterator typedef 25 2.5.4 GCPtr的构造函数 25 2.5.5 GCPtr的析构函数...