Systems CourseC/C++ · Linux · Memory
C++ CoreLesson 09 of 441 min read5 questions

Static (for Classes & Structs)

One shared copy for all objects; static methods have no `this`.

Test yourself ↓

Concept core idea Watch out easy to get wrong

Concept

Key concepts

  1. Static member variable = one shared copy for all objects, lives in static storage → does NOT add to object sizeof. Classic use: instance counter.
  2. A non-inline static data member must be defined once outside the class (int Entity::count = 0;) or → 'undefined reference'. C++17 inline static avoids the separate definition.
  3. Static member function has no this → callable as Class::fn() without an object, and can only access static members.
Watch out

Easily confused

static member vs instance member
A static member is shared by all objects (one copy); an instance member exists per object.
static method vs normal method
A static method has no this, so it can't touch per-object data — only static members. A normal method operates on a specific object.

Test yourself

5 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.