C++ static Members: Static Data, Static Functions, and inline static (C++17)

C++ static Members: Static Data, Static Functions, and inline static (C++17)

이 글의 핵심

Static members belong to the class, not each object. Learn when to define them in .cpp, inline static in C++17, and why raw static counters need synchronization.

static data members

Variables shared by all instances of the class.

class Counter {
private:
    static int count;
    
public:
    Counter() { ++count; }
    ~Counter() { --count; }
    
    static int getCount() { return count; }
};

int Counter::count = 0;

int main() {
    std::cout << Counter::getCount() << std::endl;
    Counter c1;
    std::cout << Counter::getCount() << std::endl;
    Counter c2;
    std::cout << Counter::getCount() << std::endl;
}

static member functions

Callable without an object; no this pointer.

class Math {
public:
    static int add(int a, int b) { return a + b; }
    static int multiply(int a, int b) { return a * b; }
};

int main() {
    std::cout << Math::add(3, 4) << std::endl;
}

static vs non-static

Non-static members can use instance variables and static variables. Static member functions may only use static members and names that do not require this.


Practical examples

Examples in the Korean article cover singleton (raw pointer style), object counters, a simple key/value Config, and a ShapeFactory—patterns are standard; prefer smart pointers and Meyers singleton in modern code.

static const

Integral static const can often be defined in-class; static const double may need an out-of-line definition in one .cpp depending on usage.

inline static (C++17)

class Config {
public:
    inline static int maxConnections = 100;
    inline static std::string serverName = "localhost";
};

Common problems

Initialization order across classes

Avoid cross-TU dependencies; use function-local statics when needed.

Thread safety

Protect shared static counters with std::mutex or std::atomic.

Missing definition

Every static data member declared in the class needs exactly one definition in a .cpp unless inline static (C++17) or other rules apply.


FAQ

Q1: When use static members?

A: Shared state across instances, namespaced utilities, factories.

Q2: this in static functions?

A: No—there is no instance.

Q3: Where to define static members?

A: One .cpp for non-inline static data; C++17 inline static in-class.

Q4: static vs global?

A: static members keep names inside the class—better encapsulation.

Q5: Thread-safe by default?

A: No—synchronize shared mutable statics.

Q6: Learning resources?

A: Effective C++, cppreference, C++ Primer.


  • C++ this pointer
  • C++ Initialization Order
  • C++ Linkage and Storage

Practical tips

Debugging

  • Warnings first; minimal repro.

Performance

  • Profile before optimizing.

Code review

  • Team conventions.

Practical checklist

Before coding

  • Right technique?
  • Maintainable?
  • Performance?

While coding

  • Warnings?
  • Edge cases?
  • Errors?

During review

  • Clear?
  • Tests?
  • Docs?

C++, static, member, class, OOP


  • C++ Initialization Order
  • C++ this pointer
  • C++ classes and objects
  • struct vs class
  • C++ CRTP