问题描述
我想学习如何在c ++中使用RAII。我想我知道它是什么,但不知道如何在我的程序中实现它。
有没有任何一个链接可以告诉我RAII?
没有什么(就是说,我不认为你需要一个完整的教程)。
换句话说:
指针应该被封装在智能指针类中(参见std :: auto_ptr,boost :: shared_ptr和boost :: scoped_ptr)。
需要清理的句柄应该是
同步应该依赖于在范围退出时释放mutex /同步原语(参见boost :: mutex :: scoped_lock使用示例)。
我不认为你真的可以有一个RAII教程(不再是你可以有一个设计模式)。
例如,目前我使用WinAPI进行编码,我写了以下类:
template< typename H,BOOL _stdcall CloseFunction(H)>
class checked_handle
{
public:
typedef checked_handle< H,CloseFunction> MyType;
typedef typename H HandleType;
static const HandleType NoValue;
checked_handle(const HandleType value)
:_value(value)
{
}
〜checked_handle()
{
Close();
}
HandleType * operator&()
{
return& _value;
}
operator HandleType()
{
return _value;
}
private:
HandleType _value;
void Close(const HandleType newValue = NoValue)
{
CloseFunction(_value);
_value = newValue;
}
};
template< typename H,BOOL _stdcall CloseFunction(H)>
const typename checked_handle< H,CloseFunction> :: HandleType
checked_handle< H,CloseFunction> :: NoValue =
checked_handle< H,CloseFunction> :: HandleType(INVALID_HANDLE_VALUE);
typedef checked_handle< HANDLE,:: CloseHandle> CheckedHandle;
typedef checked_handle< HWINSTA,:: CloseWindowStation> WinStationHandle;
typedef checked_handle< HDESK,:: CloseDesktop> DesktopHandle;
typedef checked_handle< HDEVNOTIFY,:: UnregisterDeviceNotification> DevNotifyHandle;
typedef checked_handle< HWND,:: DestroyWindow> WindowHandle;
BOOL __stdcall CloseKey(HKEY hKey);
typedef checked_handle< HKEY,CloseKey> RegHandle;
这个类不包括赋值语句和复制语义
以下是它的使用方法:
类声明:
class东西
{
public:
// ...
private:
WindowHandle _window;
};
此成员已分配,但我从不调用 I'd like to learn how to use RAII in c++. I think I know what it is, but have no idea how to implement it in my programs. A quick google search did not show any nice tutorials. Does any one have any nice links to teach me RAII? 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 这篇关于RAII教程C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! :: CloseWindow(_window._handle )
显式(当 Something
的实例超出作用域时会被调用( Something ::〜Something
- >
WindowHandle :: WindowHandle
- > :: Close(_window._value)
)。 / p> template<typename H, BOOL _stdcall CloseFunction(H)>
class checked_handle
{
public:
typedef checked_handle<H,CloseFunction> MyType;
typedef typename H HandleType;
static const HandleType NoValue;
checked_handle(const HandleType value)
: _value(value)
{
}
~checked_handle()
{
Close();
}
HandleType* operator &()
{
return &_value;
}
operator HandleType()
{
return _value;
}
private:
HandleType _value;
void Close(const HandleType newValue = NoValue)
{
CloseFunction(_value);
_value = newValue;
}
};
template<typename H,BOOL _stdcall CloseFunction(H)>
const typename checked_handle<H,CloseFunction>::HandleType
checked_handle<H,CloseFunction>::NoValue =
checked_handle<H,CloseFunction>::HandleType(INVALID_HANDLE_VALUE);
typedef checked_handle<HANDLE,::CloseHandle> CheckedHandle;
typedef checked_handle<HWINSTA,::CloseWindowStation> WinStationHandle;
typedef checked_handle<HDESK,::CloseDesktop> DesktopHandle;
typedef checked_handle<HDEVNOTIFY,::UnregisterDeviceNotification> DevNotifyHandle;
typedef checked_handle<HWND,::DestroyWindow> WindowHandle;
BOOL __stdcall CloseKey(HKEY hKey);
typedef checked_handle<HKEY,CloseKey> RegHandle;
class Something
{
public:
// ...
private:
WindowHandle _window;
};
::CloseWindow(_window._handle)
explicitely (it will be called when instances of Something
go out of scope (as Something::~Something
-> WindowHandle::WindowHandle
-> ::Close(_window._value)
).