C++ Core
Static (for Classes & Structs)
One shared copy for all objects; static methods have no `this`.
Concept core idea Watch out easy to get wrong
Concept
Key concepts
- Static member variable = one shared copy for all objects, lives in static storage → does NOT add to object
sizeof. Classic use: instance counter. - A non-inline static data member must be defined once outside the class (
int Entity::count = 0;) or → 'undefined reference'. C++17inline staticavoids the separate definition. - Static member function has no
this→ callable asClass::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.