Search code examples
c++staticheader-files

Static functions in header files in C++


I know that when we make a function "static", its scope is of the whole file, i.e., it can be used anywhere in that particular file, and we use it to limit the scope of the function to a particular file. What would happen if we define a header file which has some functions defined in it, all static? Would we be able to access those functions if we include that header file in some another file?


Solution

  • When you include a header, the preprocessor will replace #include directive with the file contents. After that, all rules of static apply. That is, if you include a header with static functions into some compilation units (.cpp files), each compilation unit will get its own private copy of these static functions that will be accessible only from that compilation unit.