RAII Theory && auto_ptr
RAII(Resource Acquisition is Initialization),也称为"资源获取即初始化",是C++语言的一种管理资源,避免泄露的惯用法。
C++标准保证任何情况下,已构造的对象最终会销毁,即它的析构函数最终会被调用。简单的说,RAII的做法是使用一个对象,在其构造时获取资源,在对象生命期控制对资源的访问使之始终保持有效,最后在对象析构的时候释放资源。
#include <iostream>
#include <memory>
using namespace std; class A
{
public:
A()
{
cout << "A()" << endl;
}
void xxxx()
{
cout << "xxxx" << endl;
}
~A()
{
cout << "~A" << endl;
}
}; void foo()
{
// A *p = new A;
// delete p;
auto_ptr<A> p(new A);
p->xxxx();
(*p).xxxx();
}
int main()
{
foo();
return ;
}
#include <iostream>
#include <memory>
using namespace std; class A
{
public:
A()
{
cout << "A()" << endl;
}
void xxxx()
{
cout << "xxxx" << endl;
}
~A()
{
cout << "~A" << endl;
}
}; class SmartPtr
{
public:
SmartPtr(A* pa)
{
_pa = pa;
}
A& getAref()
{
return *_pa;
}
A& operator*()
{
return *_pa;
}
A* getAp()
{
return _pa;
}
A* operator->()
{
return _pa;
}
~SmartPtr()
{
delete _pa;
}
private:
A* _pa;
}; void foo()
{
SmartPtr sp (new A);
// sp.getAref().xxxx();
// sp.getAp()->xxxx();
(*sp).xxxx();
sp->xxxx();
}
int main()
{
foo();
return ;
}
RAII Theory && auto_ptr的更多相关文章
- 对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
一.对象语义与值语义 1.值语义是指对象的拷贝与原对象无关.拷贝之后就与原对象脱离关系,彼此独立互不影响(深拷贝).比如说int,C++中的内置类型都是值语义,前面学过的三个标准库类型string,v ...
- stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结
stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...
- C++异常处理解析: 异常的引发(throw), 捕获(try catch)、异常安全
前言: C++的异常处理机制是用于将运行时错误检测和错误处理功能分离的一 种机制(符合高内聚低耦合的软件工程设计要求), 这里主要总结一下C++异常处理的基础知识, 包括基本的如何引发异常(使用th ...
- Resource Acquisition Is Initialization(RAII Idiom)
原文链接:http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization Intent ...
- C++11 auto_ptr 的问题
auto_ptr作为最早的智能指针,可以实现以RAII手法管理堆区对象,但它设计的本意只是简单的利用C++对于栈区对象的自动析构管理堆区对象, 并不像shared_ptr那样包含引用计数,可以在每次拷 ...
- [3] 智能指针std::auto_ptr
[1]std::auto_ptr 对于编译器来说,智能指针实质是一个栈对象,而并非指针类型. 智能指针通过构造函数获取堆内存的管理所有权,而在其生命期结束时,再通过析构函数释放由它所管理的堆内存. 所 ...
- std::auto_ptr
auto_ptr是C++标准库中(<utility>)为了解决资源泄漏的问题提供的一个智能指针类模板(注意:这只是一种简单的智能指针) auto_ptr的实现原理其实就是RAII,在构造的 ...
- auto_ptr浅析(转载)
转载自http://www.cnblogs.com/qytan36/archive/2010/06/28/1766555.html auto_ptr是C++标准库中(<utility>)为 ...
- 【原创】利用C++ RAII技术自动回收堆内存
[说明]这篇文章本来发布在我个人网站的博客上,但由于:1,打算以cnblogs为家了:2. 关于智能指针部分需要修订,所有将修订版发在这里,作为第一篇文章. 常遇到的动态内存回收问题 在C++的编程过 ...
随机推荐
- LeetCode_21. Merge Two Sorted Lists
21. Merge Two Sorted Lists Easy Merge two sorted linked lists and return it as a new list. The new l ...
- laravel console handle 传参方法
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use App\Libs\wxpay\CLogFile ...
- mysql使用truncate截断带有外键的表时报错--解决方案
报错内容如:1701 - Cannot truncate a table referenced in a foreign key constraint 一.为什么要使用truncate 使用trunc ...
- html设置多个div并排显示
我这里以4个div为例,html代码如下: <body> <div id="column1" style="background-color: blue ...
- python从hello world开始 - python基础入门(3)
万丈高楼平地起,编程亦如此.改变世界是结果,坚持努力学习改bug是过程,hello world是开始,所有语言均是如此. 一.使用pycharm创建第一个hello world 项目 1.Create ...
- uwp,c#,全屏播放保持屏幕响应
在开发视频app的时候,全屏播放一段时间内没有电脑操作,电脑会自动进入睡眠模式,这时就要多写些代码来保持响应了. (这里使用的是MediaElement播放控件,MediaElement需要手动添加代 ...
- linux网卡出现问题:Job for network.service failed because the control process exited with error code问题
[转自]:https://blog.csdn.net/dongfei2033/article/details/81124465 今天在centOS 7下更改完静态ip后发现network服务重启不了, ...
- 基于java配置log4j日志详解
1.Log4j 1.1了解Log4j Log4j是Apache的一个开源项目,通过使用log4j,我们可以控制日志信息输送的目的地可以是控制台.文件.GUI组件,我们也可以控制每一条日志的输出格式,通 ...
- Erlang:[笔记一,构建工具rebar之编译]
Rebar概述 Rebar是一款Erlang构建工具,使用它可以方便的编译,测试erlang程序和打包erlang发行版本.Rebar其实是一个独立的erlang脚本,默认情况下,Rebar会按照Er ...
- Error: errCode: -404011 cloud function execution error | errMsg: clou……错误
在开通了云开发之后,无论点击小程序获取openid按钮报,Error: errCode: -404011 cloud function execution error | errMsg: clou…… ...