Samuel French
Samuel French

Reputation: 665

Static Global Functions vocab

I am studying for a quiz tomorrow (c++) and had a quick question.

Global functions can be accessed anywhere, but (according to my notes), making a global function static restricts access to the defining file.

  1. Is the defining file just where the global function is located (the .cpp file)
  2. Doesn't this negate the idea of a global function

Thanks for the help again guys, you save me a lot of headaches in CSC, and I hope to return the favor when more knowledgeable.

Upvotes: 0

Views: 111

Answers (2)

Ali
Ali

Reputation: 1409

  1. Yes, making a global function static would restrict its access to the same file only (in which it is defined).

  2. Still it will be accessible to other functions within the file (its like a restricted global function). It has the benefits of minimizing name clashes to other files in the software.

Also, if you don't need a function in other files then you can restrict its access hence avoiding the chances of data corruption of global data in other files (if any).

Upvotes: 1

john
john

Reputation: 87959

  1. Yes

  2. Yes but that's sometimes what you want. It's a bit like a poor man's version of private in a C++ class declaration.

Upvotes: 1

Related Questions