Constructors and destructors
Every C++ object has a lifetime with a fixed start and a fixed end. The constructor runs when an object is created and sets it up; the destructor runs, automatically, when its lifetime ends. Follow three Cup objects through one small program, see a closing brace destroy them in the reverse order they were made, and meet RAII, the idea that makes C++ clean up after itself.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01Which of these is a constructor for a class named Mug?
A constructor has the same name as its class and no return type, not even void. ~Mug() is the destructor.
02In Cup(char c) : id(c) { ... }, when does id get its value?
The member initializer list (after the colon) initializes each member while the object is being built, before the constructor's body runs. That is why the printf in the body already sees id.
03A function declares Cup x('X'); and then Cup y('Y'); in the same block. What is printed when the block ends?
Local objects in a block are destroyed in the reverse order of their construction: y was made last, so it is destroyed first.
04In the lesson's program, why is b destroyed before c is even created?
A local object lives exactly as long as the block that declares it. b's block ends at its closing brace on main's line 5, so b's destructor runs there, before line 6 creates c.
05What does RAII mean in practice?
Resource Acquisition Is Initialization ties a resource to an object's lifetime. Because the destructor runs whenever the scope ends, even when an exception leaves it, the resource is always given back. std::vector, std::string and std::ifstream work this way.
Glossary
- Constructor
- A member function with the class's name and no return type. It runs automatically when an object is created and gives it a valid starting state.
- Member initializer list
- The part of a constructor after the colon, such as : id(c). It initializes the members before the constructor's body runs.
- Destructor
- The member function named ~ plus the class name, such as ~Cup(). It takes no arguments and runs automatically when an object's lifetime ends.
- Scope
- The part of a program where a name can be used. A pair of braces { } makes a block, which is a new scope.
- Lifetime
- The time from when an object is fully created (its constructor has finished) until its destruction starts.
- Automatic storage
- How local variables are stored, usually on the stack: memory is set aside when the block is entered and given back when it ends.
- RAII
- Resource Acquisition Is Initialization: a class acquires a resource in its constructor and releases it in its destructor, so cleanup happens on its own.
Notes and sources
- The two cards are one real file, cups.cpp, which starts with #include <cstdio> (for std::printf). Build it with g++ -std=c++20 -Wall -Wextra cups.cpp -o cups and run ./cups: it prints fill A, fill B, wash B, fill C, wash C, wash A.
- struct and class make the same kind of type; the only difference is the default access: members of a struct are public unless you say otherwise, members of a class are private.
- The cards print with std::printf from <cstdio> to keep each line short; %c prints one character. std::cout << "fill " << id << '\n'; would print the same text.
- The figure gives each object its own slot on the stack. The real layout is up to the compiler, which may reuse b's memory for c once b is gone.
- If you write no constructor at all, the compiler provides a default one; if you write no destructor, it writes one that destroys the members. Here Cup has no default constructor, because it declares Cup(char).
- Destructors of local objects also run when an exception leaves their block (stack unwinding). That is what makes RAII reliable.
- cppreference.com: Constructors and member initializer lists
- cppreference.com: Destructors
- cppreference.com: Storage class specifiers (automatic storage duration)
- cppreference.com: Scope
- cppreference.com: Lifetime
- cppreference.com: RAII
- cppreference.com: Class declaration (struct and class)
- cppreference.com: std::printf
- Standard C++ Foundation (isocpp.org): Constructors FAQ
- Standard C++ Foundation (isocpp.org): Destructors FAQ
- C++ Core Guidelines (isocpp.github.io): R.1, manage resources automatically using RAII
Quiz Unlocks after the last step.