Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Pointers and References  ›  Capstone Project

Lifetime of Objects

Lifetime of Objects 12 minAn object's lifetime starts when its constructor finishes and ends when its destructor runs, and local objects are destroyed in the reverse order of their creation.

Lifetime of Objects

When a Rocket Run level ends, the music should stop, the enemies should vanish and the save file should close, in a sensible order and without anyone remembering to call a cleanup function. C++ makes this automatic, because every object has a precise lifetime with a beginning and an end you can predict.

The rule

Objects die in reverse order of birth, at the end of their scope.

A constructor runs when an object is created, and its lifetime begins. A destructor, written ~Name(), runs when the lifetime ends. A small tracer makes the timing visible:

struct Tracer {
    std::string name;
    Tracer(std::string n) : name(n) { std::cout << "start " << name << '\n'; }
    ~Tracer() { std::cout << "end " << name << '\n'; }
};

int main() {
    Tracer level("level");
    {
        Tracer boss("boss");
    }
    Tracer music("music");
}

Rules to trace any program:

  • A local object ends at the closing brace of its scope, including on an early return.
  • Locals in the same scope end in reverse order of creation, like plates taken off a stack.
  • A temporary, such as the result of std::string("a") + "b", ends at the end of the full statement that created it.
  • A static object ends after main returns.

This is the basis of a central C++ idea: put cleanup in a destructor, and it happens on time, every time.

The shortcut

Push on construction, pop at the brace. Keep a mental stack: each constructor pushes a name, each closing brace pops the names created inside it, newest first.

Tracing mainStep 1 of 4
  1. level

    Push level. Output: start level.

The trap is handing out a reference or pointer to an object that is about to die. Returning std::string& to a local string compiles (with a warning), but the caller receives a name for something already destroyed. Return by value instead; modern compilers make that cheap.

Drills

Answer fast — trust the rule.

quick drill

In { Tracer a("a"); Tracer b("b"); }, what are the last two lines printed?

quick drill

When does a local object's destructor run?

quick drill

Which function safely gives the caller a newly built player name?

quick drill

When is a function's static local object destroyed?

Remember

  • Objects die in reverse order of birth, at the end of their scope.
  • Shortcut: push on construction, pop at the brace, newest first.
  • Trap: never return a reference or pointer to a local; return by value.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTracing start and end

Rocket Run's main creates Tracer level("level"), then an inner block { Tracer boss("boss"); }, then Tracer music("music"). Each Tracer prints "start" plus its name in its constructor and "end" plus its name in its destructor. Give the six lines of output in order and explain the rule behind them.