From http://blog.csdn.net/Monkeycn/article/details/5817138
Static:
1. Is this idea correct if variables other than functions are global variables?
A: In C, it is completely correct. Only static modification can be divided into static global variables and general global variables.
2. What is the use of static before the global variable?
A: In C, static global variables mean two aspects. 1. In terms of life cycle, like general global variables, they coexist with the entire program. 2. In terms of visibility, they are only visible in the compilation unit that defines it. For example, in Testa. c defines a static global variable X: static int X; then you can only. the function of C references it in another file testb. if you want to use it in C: extern int X; and then use X in a function, the compiler generally reports an error during the connection phase that the X symbol cannot be found.
3. What is the use of static before a function?
A: It is similar to static global variables. Simply put, in Testa. static function defined in C: static void a (void) cannot be in testb. c function calls. If this is required, the compiler will return an error during connection.
Static purpose: Generally, it imposes static restrictions on non-interface functions and global variables that are not used externally. This ensures that these variables will not be used illegally by other compilation units, it can also avoid conflicts with the same name symbols in other compilation units. It is used in many drivers.
Anonymous space:
When defining a namespace, you can ignore the namespace Name:
namespce {char c;int i;double d;}
The compiler internally generates a unique name for the namespace and generates a using command for the anonymous namespace. Therefore, the above Code is equivalent:
namespace __UNIQUE_NAME_ {char c;int i;double d;}using namespace __UNIQUE_NAME_;
The name declared in the anonymous namespace will also be converted by the compiler and bound to the unique internal name (here _ unique_name _) generated by the compiler for this anonymous namespace. Another important thing is that these names have internal link attributes, which are the same as the Link Attributes of global names whose names are static, that is, the name scope is restricted in the current file, cannot be linked by using the extern declaration in another file. If you do not advocate using Global static to declare that a name has an internal link attribute, the anonymous namespace can be used as a better way to achieve the same effect.
Note: The namespace has the external connection attribute, but the _ unique_name _ generated by the anonymous namespace cannot be obtained in other files. The unique name is invisible.
In the new C ++ standard, anonymous namespaces are recommended instead of static. Static is used in different places and has different meanings, which may cause confusion. In addition, static cannot modify the class.