Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ function prototypes

Tags:

c++

prototype

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);
like image 532
John Avatar asked Dec 03 '22 03:12

John


1 Answers

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.

like image 157
Billy ONeal Avatar answered Dec 22 '22 15:12

Billy ONeal