RAII Idiom Example

Learn about RAII with some coding examples.

We'll cover the following...

Example

In this lesson, we’ll take a look at an example and see how a destructor is called when the resource goes out of scope. Let’s look at its code.

Code

Press + to interact
#include <iostream>
#include <new>
#include <string>
class ResourceGuard{
private:
const std::string resource;
public:
ResourceGuard(const std::string& res):resource(res){
std::cout << "Acquire the " << resource << "." << '\n';
}
~ResourceGuard(){
std::cout << "Release the "<< resource << "." << '\n';
}
};
int main(){
std::cout << '\n';
ResourceGuard resGuard1{"memoryBlock1"};
std::cout << "\nBefore local scope" << '\n';
{
ResourceGuard resGuard2{"memoryBlock2"};
}
std::cout << "After local scope" << '\n';
std::cout << '\n';
std::cout << "\nBefore try-catch block" << '\n';
try{
ResourceGuard resGuard3{"memoryBlock3"};
throw std::bad_alloc();
}
catch (std::bad_alloc& e){
std::cout << e.what();
}
std::cout << "\nAfter try-catch block" << '\n';
std::cout << '\n';
}

Code explanation

Let’s look at the explanation of the code ...