[2026] C++ static Members: Static Data, Static Functions, and inline static (C++17)
이 글의 핵심
Class static members shared by all instances: declaration vs definition, ODR, thread safety, singletons, factories, and C++17 inline static in headers.
static data members
Variables shared by all instances of the class. 다음은 cpp를 활용한 상세한 구현 코드입니다. 클래스를 정의하여 데이터와 기능을 캡슐화하며. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.
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.
아래 코드는 cpp를 사용한 구현 예제입니다. 클래스를 정의하여 데이터와 기능을 캡슐화하며. 코드를 직접 실행해보면서 동작을 확인해보세요.
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)
아래 코드는 cpp를 사용한 구현 예제입니다. 클래스를 정의하여 데이터와 기능을 캡슐화하며. 코드를 직접 실행해보면서 동작을 확인해보세요.
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.
Related posts (internal links)
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?