Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 11 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
没有什么(也就是说,我认为你不需要完整的教程)。
RAII 可以简单地解释为“每个需要清理的资源都应该交给对象的构造函数”。
换句话说:
指针应该封装在智能指针类中(例如,参见 std::auto_ptr、boost::shared_ptr 和 boost::scoped_ptr)。
需要清理的句柄应该封装在类中,在销毁时自动释放/释放句柄。
同步应该依赖于作用域退出时释放互斥/同步原语(有关示例,请参阅 boost::mutex::scoped_lock 用法)。
我不认为您真的可以拥有有关 RAII 的教程(例如,您无法拥有有关设计模式的教程)。 RAII 更多的是一种查看资源的方式。
例如,目前我正在使用 WinAPI 进行编码,并且编写了以下类:
该类不包括赋值和复制语义(我删除了它们以提供一个最小的示例),因此按值返回将导致句柄关闭了两次。
它的使用方式如下:
类声明:
该成员已分配,但我从不显式调用
::CloseWindow(_window._handle)
(当Something
实例消失时,它将被调用范围(如Something::~Something
->WindowHandle::WindowHandle
->::Close(_window._value)
)。There's nothing to it (that is, I don't think you need a full tutorial).
RAII can be shortly explained as "Every resource requiring cleanup should be given to an object's constructor."
In other words:
Pointers should be encapsulated in smart pointer classes (see std::auto_ptr, boost::shared_ptr and boost::scoped_ptr for examples).
Handles requiring cleanup should be encapsulated in classes that automatically free/release the handles upon destruction.
Synchronization should rely on releasing the mutex/synchronization primitive upon scope exit (see boost::mutex::scoped_lock usage for an example).
I don't think you can really have a tutorial on RAII (not anymore than you can have one on design patterns for example). RAII is more of a way of looking at resources than anything else.
For example, at the moment I'm coding using WinAPI and I wrote the following class:
This class doesn't include assignment and copy semantics (I removed them to provide a minimal example) so returning by value, will cause the handles to be closed twice.
Here's how it's used:
class declaration:
This member is allocated but I never call
::CloseWindow(_window._handle)
explicitely (it will be called when instances ofSomething
go out of scope (asSomething::~Something
->WindowHandle::WindowHandle
->::Close(_window._value)
).wikipedia 的解释还不错。
The wikipedia explanation isn't bad.
我个人发现关于 RAII 主题最有帮助的参考文献是Exceptional C++< /a> 作者:赫伯·萨特。
萨特的《本周大师》文章中涉及到该书中涵盖的许多主题。这些文章可在 http://gotw.ca/gotw/index.htm 上找到。
The reference that I personally have found most helpful on the topic of RAII is the book Exceptional C++ by Herb Sutter.
Many of the topics covered in that book are touched on in the Guru of the Week articles by Sutter. Those articles are available at http://gotw.ca/gotw/index.htm.
“有效的 C+” 的第 13 项也非常有用
Item 13 of "Effective C+" is also pretty useful