What is the difference between static global and non-static global identifier in C++?

非 Y 不嫁゛ 提交于 2019-12-31 08:28:47

问题


What is the difference between static global and non-static global identifier in C++?


回答1:


Static limits the scope of the variable to the same translation unit.
A static global variable has internal linkage.
A non-static global variable has external linkage.

Good Read:
What is external linkage and internal linkage?




回答2:


A global static variable is only available in the translation unit (i.e. source file) the variable is in. A non-static global variable can be referenced from other source files.




回答3:


If you don't know what the difference is, correct answer will probably be even more confusing to you. In short, statics of a class aren't realted to statics at file scope. Statics of a class are esentially identical to regular variables, but they will have to be referenced by prefixing them with class name. Statics at file scope are regular variables that are local to the file only. To understand what that means, try to add two variables with the same name into a single project. You will get linker errors because there are multiple identical symbols. By making symbols static you will avoid that problems and variable's name won't be accessible from outside the file.




回答4:


Global Non static variables are accessable from other files whereas static global variables are not



来源:https://stackoverflow.com/questions/13162092/what-is-the-difference-between-static-global-and-non-static-global-identifier-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!