What is meant by resource acquisition and initialization RAII?

Entered the RAII idiom, but does not delve into these words, "resource acquisition is initialization".

I would be grateful if someone on the examples will show that it is receiving, and that the resource itself? And how does it "initialize"? Commercials say - step by step.
July 9th 19 at 13:30
1 answer
July 9th 19 at 13:32
RAII is to wrap up the class some "raw" nature. An entity can be any, but it is awkward to work with because it is necessary to monitor the creation and destruction in the program. For example

class WinAPI32_Window
HWND hWnd = 0; // <--resource
 //100 lines for window class registration and window creation
 hWnd = CreateWindow(/* many parameters */); // <--get a resource

 if (!hWnd) throw std::system_error("win creation error"); // throw exception in the constructor if the window is not created, the object will not be created also 
 ShowWindow(hWnd, nCmdShow);

 DestroyWindow(hWnd); // <-- release the resource

//well, further in the program:
try {
 auto win = new WinAPI32_Window; //create an object = initialize and get the hWnd inside
} catch //...
delete win; //free the resource by deleting the object
Now it is clear, thank you! - Jeremy_Bartoletti commented on July 9th 19 at 13:35

Find more questions by tags C++