Using static in the file domain to restrict access to the current translation system is deprecated in C ++, but still acceptable in C.
Use an unnamed namespace instead
namespace { int file_scope_x; }
Variables declared in this way are available only in the file, as if they were declared static.
The main cause of obsolescence is the deletion of one of several overloaded static keyword values.
Initially, this meant that a variable, for example, in a function, would be provided with storage for the program lifetime in the area for such variables and would not be stored on the stack, as usual for local function variables.
Then the keyword was overloaded to apply to file binding. It is not advisable to compose new keywords as needed, as they may violate existing code. Thus, this one was used again with a different value, without causing conflicts, because the variable declared as static cannot be both inside the function and at the top level, and the functions did not have a modifier before. (Storage consolidation is completely lost when accessing functions, because they are not stored anywhere.)
When classes came in C ++ (and in Java and C #), the keyword was used again, but the value is at least closer to the original intent. Variables declared in this way are stored in the global scope, and not on the stack, both for function variables and in the heap for members of an object. Since variables cannot be both at the top level and inside the class definition, additional meaning can be uniquely tied to class variables. They can be referenced only through the class name or inside an object of this class.
UncleO Jun 03 '09 at 6:44 2009-06-03 06:44
source share