A static function in C++ is a function declared with the static keyword, and the keyword means two different things depending on where you use it. Inside a class, it makes a static member function that belongs to the class and needs no object. At namespace (file) scope, it gives the function internal linkage, so only the current translation unit can call it.
Modern C++ still supports both uses, but for file-local functions an unnamed namespace is the preferred style.
A static member function is called through the class name. It has no this pointer, so it can only use static data members, other static member functions, and anything passed in as an argument.
inline static (C++17) lets you define the data member inside the class. Before C++17 you needed a separate definition such as int Counter::count_ = 0; in one source file. A static member function can also be called with an object (c.total()), but writing Counter::total() makes the intent clear.
When you put static on a free function, the function gets internal linkage. Another source file cannot call it, and a function with the same name in another file does not clash with it.
log_line exists only inside helpers.cpp. Other files can call do_work, which has external linkage by default.
The C++ standard has long preferred unnamed namespaces to static for file-local entities. They work for types, variables and functions alike.
Both forms give internal linkage, and both are valid in C++20 and C++23. Use whichever your codebase already uses, but prefer the unnamed namespace in new code because it also covers type definitions.
| Kind | Needs an object | Has this | Can touch instance data | Linkage |
|---|---|---|---|---|
| Regular member function | Yes | Yes | Yes | External |
| Static member function | No | No | Only through an object it receives | External |
File-level static function | Not applicable | Not applicable | Not applicable | Internal |
A static member function can still reach private instance data if you pass an object to it, because it is part of the class and has access rights.
Widget::create() that return a configured object.Temperature::fromFahrenheit(double).Avoid putting mutable static state behind static functions without thinking about threads. Several threads calling Counter::create() above would race on count_. Use std::atomic<int> or a mutex when the code can run concurrently.
this or a non-static member inside a static member function. This does not compile.static on the out-of-class definition of a static member function. Only the declaration inside the class carries static.static function in a header. Each file that includes the header gets its own copy, which is rarely what you want. Use inline for header-defined functions.Inside a class it means the function is not tied to an object. Outside a class it means the function is visible only in its own source file.
No. A static member function cannot be virtual, and it cannot be const qualified because there is no object to be const.
Not quite. A global function has external linkage and can be called from any file that declares it. A file-level static function has internal linkage and is private to its file.
Both work. An unnamed namespace is the style recommended by most modern C++ guidance because it also hides types and variables, while static only applies to functions and variables.
The static keyword on a C++ function either ties it to the class instead of an object or hides it inside one translation unit. Static member functions have no this pointer and are called with Class::function(). File-level static functions, or functions in an unnamed namespace, keep helper code private and avoid name clashes.