RAII -- Resource Acquisition Is Initialization
2009-11-06 16:59
363 查看
RAII,资源获取即初始化。
这个东西本不是什么高深的东西,日常写代码时也经常用到,其实就是将资源与对象的生命周期绑定,具体的做法就是在构造函数中初始话资源,在析构函数中释放资源,这样使用这个对象的时候甚至无需手动对其进行任何内存操作,从而保证内存不会泄露。
wiki的一段:
Resource Acquisition Is Initialization, often referred to by the acronym RAII (or, erroneously, RIIA), is a popular design pattern in several object oriented programming languages like C++, D and Ada. The technique was invented by Bjarne Stroustrup,[1] to deal with resource deallocation in C++. In this language, the only code that can be guaranteed to be executed after an exception is thrown are the destructors of objects residing on the stack. Resources therefore need to be tied to the lifespan of suitable objects. They are acquired during initialization, when there is no chance of them being used before they are available, and released with the destruction of the same objects, which is guaranteed to take place even in case of errors.
RAII is vital in writing exception-safe C++ code: to release resources before permitting exceptions to propagate (in order to avoid resource leaks) one can write appropriate destructors once rather than dispersing and duplicating cleanup logic between exception handling blocks that may or may not be executed.
http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
这个东西本不是什么高深的东西,日常写代码时也经常用到,其实就是将资源与对象的生命周期绑定,具体的做法就是在构造函数中初始话资源,在析构函数中释放资源,这样使用这个对象的时候甚至无需手动对其进行任何内存操作,从而保证内存不会泄露。
wiki的一段:
Resource Acquisition Is Initialization, often referred to by the acronym RAII (or, erroneously, RIIA), is a popular design pattern in several object oriented programming languages like C++, D and Ada. The technique was invented by Bjarne Stroustrup,[1] to deal with resource deallocation in C++. In this language, the only code that can be guaranteed to be executed after an exception is thrown are the destructors of objects residing on the stack. Resources therefore need to be tied to the lifespan of suitable objects. They are acquired during initialization, when there is no chance of them being used before they are available, and released with the destruction of the same objects, which is guaranteed to take place even in case of errors.
RAII is vital in writing exception-safe C++ code: to release resources before permitting exceptions to propagate (in order to avoid resource leaks) one can write appropriate destructors once rather than dispersing and duplicating cleanup logic between exception handling blocks that may or may not be executed.
http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
相关文章推荐
- The RAII(resource acquisition is initialization) of C++
- RAII(Resource Acquisition Is Initialization:资源获取即初始化)
- Resource Acquisition Is Initialization(RAII Idiom)
- 浅谈----RAII资源获得即初始化(Resource Acquisition Is Initialization)
- Resource Acquisition Is Initialization(RAII Idiom)
- RAII(Resource Acquisition Is Initialization资源获取即初始化 )思想
- Resource Acquisition Is Initialization(RAII Idiom)
- Resource Acquisition Is Initialization(RAII)
- Resource Acquisition Is Initialization (RAII)是什么?(封装的优势)
- RAII(Resource Acquisition Is Initialization)资源获得式初始化
- RAII:资源获取即初始化(resourse acquisition is initialization)
- C++ “resource acquisition is initialization”
- RAII (Resource Acquirement Is Initialization)
- No 'Access-Control-Allow-Origin' header is present on the requested resource.
- resource is out of sync with the file system解决方法
- eclipse: Resource is out of sync with the file system
- Android 程序运行崩溃:Resource is not a drawable (color or path)
- Resource is out of sync with the file system查看项目文件时解决办法
- HTTP Status 404(The requested resource is not available)的几种解决方案
- resource is out of sync with the file system