STL之auto_ptr智能指针

97 篇文章 0 订阅
92 篇文章 0 订阅

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();

	};
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
探索全栈前端技术的魅力:HTML+CSS+JS+JQ+Bootstrap网站源码深度解析 在这个数字化时代,构建一个既美观又功能强大的网站成为了许多开发者和企业追逐的目标。本份资源精心汇集了一套完整网站源码,融合了HTML的骨架搭建、CSS的视觉美化、JavaScript的交互逻辑、jQuery的高效操作以及Bootstrap的响应式设计,全方位揭秘了现代网页开发的精髓。 HTML,作为网页的基础,它构建了信息的框架;CSS则赋予网页生动的外观,让设计创意跃然屏上;JavaScript的加入,使网站拥有了灵动的交互体验;jQuery,作为JavaScript的强力辅助,简化了DOM操作与事件处理,让编码更为高效;而Bootstrap的融入,则确保了网站在不同设备上的完美呈现,响应式设计让访问无界限。 通过这份源码,你将: 学习如何高效组织HTML结构,提升页面加载速度与SEO友好度; 掌握CSS高级技巧,如Flexbox与Grid布局,打造适应各种屏幕的视觉盛宴; 理解JavaScript核心概念,动手实现动画、表单验证等动态效果; 利用jQuery插件快速增强用户体验,实现滑动效果、Ajax请求等; 深入Bootstrap框架,掌握移动优先的开发策略,响应式设计信手拈来。 无论是前端开发新手渴望系统学习,还是资深开发者寻求灵感与实用技巧,这份资源都是不可多得的宝藏。立即深入了解,开启你的全栈前端探索之旅,让每一个网页都成为技术与艺术的完美融合!
探索全栈前端技术的魅力:HTML+CSS+JS+JQ+Bootstrap网站源码深度解析 在这个数字化时代,构建一个既美观又功能强大的网站成为了许多开发者和企业追逐的目标。本份资源精心汇集了一套完整网站源码,融合了HTML的骨架搭建、CSS的视觉美化、JavaScript的交互逻辑、jQuery的高效操作以及Bootstrap的响应式设计,全方位揭秘了现代网页开发的精髓。 HTML,作为网页的基础,它构建了信息的框架;CSS则赋予网页生动的外观,让设计创意跃然屏上;JavaScript的加入,使网站拥有了灵动的交互体验;jQuery,作为JavaScript的强力辅助,简化了DOM操作与事件处理,让编码更为高效;而Bootstrap的融入,则确保了网站在不同设备上的完美呈现,响应式设计让访问无界限。 通过这份源码,你将: 学习如何高效组织HTML结构,提升页面加载速度与SEO友好度; 掌握CSS高级技巧,如Flexbox与Grid布局,打造适应各种屏幕的视觉盛宴; 理解JavaScript核心概念,动手实现动画、表单验证等动态效果; 利用jQuery插件快速增强用户体验,实现滑动效果、Ajax请求等; 深入Bootstrap框架,掌握移动优先的开发策略,响应式设计信手拈来。 无论是前端开发新手渴望系统学习,还是资深开发者寻求灵感与实用技巧,这份资源都是不可多得的宝藏。立即深入了解,开启你的全栈前端探索之旅,让每一个网页都成为技术与艺术的完美融合!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值