如何在一个工程中定义一些很多文件都需要使用的变量?
在一个工程中 ,比如 类 class1 class2, 和 class3 class 4这几个类的cpp文件都需要用到变量 int insider;
现在我做的就是在 class1的cpp文件一开头定义(不写到类里面) : int insider; 然后在其它的几个类的cpp文件的开头写上
extern int insider;
例如:
class1.cpp
int insider; class1::class1(){ ....}extern int insider; class1::class1(){ ....}extern int insider; class1::class1(){ ....}extern int insider; class1::class1(){ ....}// CommonDefine.hclass CCommonDefine{public: static CCommonDefine* GetInstance() { static CCommonDefine s_Define; return &s_Define; }private: CCommonDefine() : m_iInsider(0) {} ~CCommonDefine() {}public: int m_iInsider;};
[解决办法]
一般这种情况我是放到main.cc里面弄为全局变量其他地方用extern