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的更多相关文章

  1. 对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector

    一.对象语义与值语义 1.值语义是指对象的拷贝与原对象无关.拷贝之后就与原对象脱离关系,彼此独立互不影响(深拷贝).比如说int,C++中的内置类型都是值语义,前面学过的三个标准库类型string,v ...

  2. stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结

    stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...

  3. C++异常处理解析: 异常的引发(throw), 捕获(try catch)、异常安全

    前言: C++的异常处理机制是用于将运行时错误检测和错误处理功能分离的一 种机制(符合高内聚低耦合的软件工程设计要求),  这里主要总结一下C++异常处理的基础知识, 包括基本的如何引发异常(使用th ...

  4. Resource Acquisition Is Initialization(RAII Idiom)

    原文链接:http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization Intent ...

  5. C++11 auto_ptr 的问题

    auto_ptr作为最早的智能指针,可以实现以RAII手法管理堆区对象,但它设计的本意只是简单的利用C++对于栈区对象的自动析构管理堆区对象, 并不像shared_ptr那样包含引用计数,可以在每次拷 ...

  6. [3] 智能指针std::auto_ptr

    [1]std::auto_ptr 对于编译器来说,智能指针实质是一个栈对象,而并非指针类型. 智能指针通过构造函数获取堆内存的管理所有权,而在其生命期结束时,再通过析构函数释放由它所管理的堆内存. 所 ...

  7. std::auto_ptr

    auto_ptr是C++标准库中(<utility>)为了解决资源泄漏的问题提供的一个智能指针类模板(注意:这只是一种简单的智能指针) auto_ptr的实现原理其实就是RAII,在构造的 ...

  8. auto_ptr浅析(转载)

    转载自http://www.cnblogs.com/qytan36/archive/2010/06/28/1766555.html auto_ptr是C++标准库中(<utility>)为 ...

  9. 【原创】利用C++ RAII技术自动回收堆内存

    [说明]这篇文章本来发布在我个人网站的博客上,但由于:1,打算以cnblogs为家了:2. 关于智能指针部分需要修订,所有将修订版发在这里,作为第一篇文章. 常遇到的动态内存回收问题 在C++的编程过 ...

随机推荐

  1. CentOS7下搭建zabbix监控(四)——Zabbix报警设置

    CentOS7下搭建zabbix监控(一)——Zabbix监控端配置 CentOS7下搭建zabbix监控(二)——Zabbix被监控端配置 CentOS7下搭建zabbix监控(三)——Zabbix ...

  2. 123457123457#0#-----com.yuming.ZuiNiuChengYu--前拼后广--最牛成语

    com.yuming.ZuiNiuChengYu--前拼后广--最牛成语

  3. LeetCode_67. Add Binary

    67. Add Binary Easy Given two binary strings, return their sum (also a binary string). The input str ...

  4. sqlserver 数据库操作导出数据sql工具

    软件名称sqldbx 下载URL  https://download.csdn.net/download/yanghl1998/7832861 Navicat Premium  这个工具任何类型数据库 ...

  5. HTML5+CSS3系列教程——如何制作简单按钮笔记

      1.按钮的制作方式 用图片(目前用的不多) 纯CSS a标签 input 图片二决定了input的类型 当input的type属性是submit button等这些的时候他呈现一个按钮 butto ...

  6. vue+element项目中过滤输入框特殊字符小结

    可以在main.js中写入方法 Vue.prototype.validSe = function (value, number = 255) { value = value.replace(/[`-* ...

  7. 封装一个Model或者Vender类

    Model <?php /** * User: Eden * Date: 2019/3/21 * 共有内容 */ class WxPayModel extends Model { protect ...

  8. PCL学习(一)从PLY文件读入点云数据

    #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #inclu ...

  9. 防火墙firewalld报错:ERROR: Exception DBusException: org.freedesktop.DBus.Error.AccessDenied:...

    ERROR: Exception DBusException: org.freedesktop.DBus.Error.AccessDenied: Connection ":1.1985&qu ...

  10. python之 -> 的含义

    函数或方法标注通常用于 :类型提示:例如以下函数预期接受两个 int 参数并预期返回一个 int 值: def sum(a: int, b: int) -> int: return a + b ...