[英]using extern keyword to declare variables in header files / c++
What is the proper way of declaring variables in the header file in c++? 在C ++中的头文件中声明变量的正确方法是什么? And if it is not a good idea, then why?
如果不是一个好主意,那为什么呢? Thank you.
谢谢。
The correct way would be to declare the variable with the extern keyword in the header file and then you have to declare it in one (!) cpp file without the extern keyword. 正确的方法是在头文件中使用extern关键字声明变量,然后必须在一个没有extern关键字的(!)cpp文件中声明变量。
But: 但:
Variables in header files are global variables. 头文件中的变量是全局变量。 These have much problems.
这些有很多问题。 Here a few:
这里有几个:
You should never use global variables in C++. 您永远不要在C ++中使用全局变量。 They are only there for backward compatibility with C.
它们仅是为了与C向后兼容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.