Reputation: 1229
Could someone tell me what's the purpose of declaring static functions outside classes? What's the difference between this 2? Are there any benefits for using static in this situation?
static void someRandomFunction();
int main()
{
someRandomFunction();
return 0;
}
and
void someRandomFunction();
int main()
{
someRandomFunction();
return 0;
}
Upvotes: 79
Views: 52336
Reputation: 1
Using static
outside of a class
or struct
means that the linkage of the variable/function declared static
, is going to be internal. It is only going to be visible in that translation unit (source file).
So, it would be a good practice to mark the functions and variables as static
, until unless you need that in external/other translation unit.
Upvotes: -1
Reputation: 28872
static
tells the compiler not add the function to the symbol table for the object file. This effectively means that the linker is unable to find the function which in turn means you can only use the function directly in the current compilation unit. You can however call static functions from another compilation unit if this is done through a function pointer.
Upvotes: 3
Reputation: 17265
A static
function remains visible only in file scope. This is a C feature.
The recommended way to do it in C++ is using an anonymous namespace, as in:
namespace // no name, i.e. anonymous
{
void someRandomFunction();
}
int main()
{
someRandomFunction(); // visible only within this file.
return 0;
}
Note that the function body also has to be declared somewhere within the same file since the linker will not try to find it in other (external) translation units.
So void someRandomFunction();
is really a forward declaration for a function that is defined elsewhere in the same file (i.e. in the same translation unit).
If the function is actually called, you will get a linking error unless the function body is defined in the same file.
(The more pedantic technical term is actually not file but translation-unit since the body might be in an #include
ed header do not in the actual file per-se. )
Upvotes: 20
Reputation: 851
Static methods and static functions are entirely different things.
Static methods are methods of a class instead of an instance (which you already know, as it seems).
Static functions, on the other hand, are function which are available only in the module they are defined in. They are not exported and cannot be put in a header file and used in another c file. This way you can write different functions sharing the same name, and also the compiler may optimize your code more thoroughly by inlining the function, knowing that no other file is dependant on it.
Upvotes: 4
Reputation: 7118
static void someRandomFunction();
This has to be used within same compilation unit (source file) and outside that compilation unit, not available for use. Whereas, if you have
void someRandomFunction();
with one definition acrosss the program, the function can be used by any compilation unit globally across the program
Upvotes: 4
Reputation: 254431
At namespace scope, static
gives a name internal linkage, meaning that it is only accessible within the translation unit that contains the definition. Without static
, it has external linkage, and is accessible in any translation unit.
So you'd use static
(or, alternatively, an unnamed namespace) when writing a function that's only intended for use within this unit; the internal linkage means that other units can define different functions with the same name without causing naming conflicts.
Non-static functions (and global names in general) are better declared in a header, to make sure that every translation unit that uses them gets the same declaration.
Upvotes: 82
Reputation: 409166
The static
keyword on global functions or variables limits the visibility and linkage scope of the function or variable to the current translation unit.
That means that for a function, it can only be called from the current source file, and not from other source files.
Upvotes: 21