C/C++ Arena

Constructors, destructors and scope

Each Noisy prints when it's made and when it's destroyed. Watch the output: b is destroyed at the end of the inner block, before c is even created, and at the end of main the rest go in reverse order.

This automatic cleanup is what RAII builds on: put the release in the destructor and it can't be forgotten.

#include <iostream>
#include <string>

struct Noisy {
    std::string name;
    explicit Noisy(std::string n) : name(n) {
        std::cout << "make " << name << "\n";
    }
    ~Noisy() {
        std::cout << "drop " << name << "\n";
    }
};

int main() {
    Noisy a("a");
    {
        Noisy b("b");
    }
    Noisy c("c");
    return 0;
}

Output:

make a
make b
drop b
make c
drop c
drop a

From the lesson: Lifetime and RAII