1. 程式人生 > >C++是定義全域性變數和函式常用方法.

C++是定義全域性變數和函式常用方法.

1:在標頭檔案是宣告變數,然後在使用的檔案中用exten標識。

".h":
int ntemp;
".cpp":
exten int ntemp;

2:定義一個全域性變數類,使用時包含標頭檔案即可:

"GlobalVar.h"
Class CGlobalVar
{
            CGlobalVr();
            ~CGlobal();
            static int ntemp;  //用static
};
"GlobalVar.cpp"
int CGlobalVar::ntemp = 0;//定義並初始化
CGlobalVar::CGlobalVar()
{

}
CGlobalVar::~CGlobalVar()
{

}

3:全域性函式可以用上面2的做法外還可以:

在".h"裡宣告
int GetDataTime(CString strDataTime, CString strSep);
在".cpp"裡實現
int GetDataTime(CString strDataTime, CString strSep)
{
       ....
}

使用時只是將".h"檔案包含進去即可。