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?

1
  • 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. Commented Apr 18, 2020 at 15:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.