I have just started learning C++. Can some explain the difference between the following C++ function prototypes?
void f(int n);
extern void f(int n);
static void f(int n);
The void and extern void versions are the same. They indicate that the function has external linkage (i.e. the definition of the function may be expected to come from some other C or C++ file). Static indicates that the function has internal linkage, and will exist only in the current C++ file.
You almost never see these specifiers applied to functions because 99.9% of the time you want the default extern
behavior.
You may see the static
or extern
storage specifiers on global variables though, which is often done to reduce name conflicts with other files in the same project. This is a holdover from C; if you're using C++ this kind of thing should be done with an anonymous namespace instead of static
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With