+ 3
RAII stands for "Resource Acquisition Is Initialization", and is more a coding idiom regarding the acquisition and release of resources by a C++ program. It is not some class or function implemented in Std or Boost lib. The main idea is this: you acquire the resource in the ctor of a class and you release the resource in the dtor. So you are basically using a class (or struct) to wrap the resource. Now you can use the lifetime of an object of the class to implicitly manage the lifetime of the resource. Something like: class DeviceContext { DeviceContext() { hDC = CreateDC(..) ; } ~DeviceContext() { ReleaseDC(hDC) ; } HDC hDC; }; It should be clear that the lifetime of hDC is tied to the lifetime of a DeviceContext instance and that the class is just a thin wrapper around the resource. This can be quite useful, especially if your resource is complicated to create and/or destroy.
30th Mar 2017, 3:39 PM
Ettienne Gilbert
Ettienne Gilbert - avatar