c++ - How to store global variables?
怪我咯
怪我咯 2017-07-03 11:42:03
0
1
1320

First ask a question
It is said in books and online that the scope of a variable after adding static is the current file, and without adding scope it is the entire project
Does it mean that the variable with static added can only be used in this file? ?

I use a file to store global variables. I need to use this global variable in multiple places. According to the book, it should not be static, but

will appear.
错误    LNK2005    1    "XXX" (XXX) 已经在 XXX 

There is no problem if I add static

I am using VS2017

Please explain this problem. Can I use this method to store the global data?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
三叔

Does it mean that variables added with static can only be used in this file? Yes.

You can use global variables however you want in normal code, but in actual projects, global variables can be used as long as you can, remember this. So your approach of "putting all global variables in one file" is not advisable.

Your error report should be due to redefinition. The global variable is redefined during the obj link process, because the global variable is an external link attribute, which is visible to all files.

Which file needs this variable, you can make it static. If other files need the value of this variable, you can put this variable into the class to make it a class static member variable, and then directly include the file, referenced by A::x That’s it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template