2

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?

Pratham Yadav
  • 69
  • 1
  • 8
  • 1
    Note that you can also use an anonymous/unnamed namespace to make functions local to a translation unit. `static` is not the only option. – Jesper Juhl Apr 18 '20 at 15:01

1 Answers1

7

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.

Evg
  • 25,259
  • 5
  • 41
  • 83