- Example

The example in this lesson shows the deterministic behavior of RAII in C++.

We'll cover the following

Example - RAII

RAII stands for Resource Acquisition Is Initialization, and it is one of the most important idioms in C++. It states that a resource should be acquired in the constructor of the object and released in the destructor of the object. It is important to remember that the destructor will automatically be called if the object goes out of scope.

Is this not deterministic? In Java or Python (__del__), you have a destructor but not the guarantee. Therefore, if you use the destructor to release a critical resource, such as a lock, it can end disastrously. In C++, however, this problem is prevented.

Look at the example below:

Get hands-on with 1000+ tech skills courses.