In most C++ implementations, each .cpp
file is compiled into a corresponding .o
file. The .o
file contains the object code for the functions of the .cpp
file -- i.e. the machine-code instructions the CPU will need to execute in order to execute those functions.
For non-static functions, the .o
file also contains linker-readable metadata; i.e. a list of (mangled) function names that are present in the .o
file, where those functions are located within the file, etc. At link-time, the linker will use this metadata to resolve calls to these functions that were made from within other .cpp
files.
For static functions, OTOH, the .o
file does not contain any metadata; as far as the linker is concerned, the static functions do not exist. The static functions are effectively "secret", available only to the other code from the same .cpp
file (which does not depend on the linker to gain access to the static functions as it is compiled as part of the same compilation-unit and can therefore just reference them directly)
In your case you implemented your static function in a .h
file, which means that a separate copy of the function is being included in the .o
file of every .cpp
file that uses that function, which is why it still "works" for you.
People often use static
as a sort of private-namespace marker to guarantee that functions won't be called from outside of the .cpp
file they appear in. In C++, an anonymous namespace can be used to achieve a similar result.