Declaring static global functions in header files

I came across a piece of code written by someone else. There are several global functions declared as static in the header files. The functions themselves are defined in separate implementation files. AFAIK, the definition of a static function is not visible outside the translation unit where the function is defined. If so, what is the point of declaring static functions in header files?

// in some header file static void foo(); // in some implementation file static void foo() { .... .... } 
+8
c ++ c
source share
1 answer

Well, declared static functions are only visible in the source file in which they are defined. Although listing them in a separate headline is not a good idea. I also saw some cases where developers did this. They do this to arrange them in order so that they can call one function from another. Here is what I mean:

 /* In header */ static void plus(int); static void minus(int); static void multiply(int); /* In source file */ static void minus(int v) { /* So plus can be called in minus without having to define it * before minus */ plus(); } static void plus(int v) { /* code */ } 

But IMHO this is a pretty disastrous way to do this. The best solution is to simply prototype the static functions in the source file before implementing them.

+6
source share

All Articles