在项目组成员中发现有小伙伴在定义全局静态变量时在头文件中。请引起足够的注意!!!
 有些刚毕业或者工作几年的小伙伴基础不牢的还是要好好的记忆下,例如const、static、register、volatile等关键字的使用及注意事项,避免在开发过程中引起异常。
testA.h
static int x = 1;
void printAx();testA.cpp
#include "stdafx.h"
#include "testA.h"
void printAx()
{
    int nTmp = x++;   
    printf("A.cpp::&x=[%p] 默认值x=[%d] ++之后值x=[%d]\n", &x, nTmp, x);
}testB.h
static int y = 1;
void printBx();testB.cpp
#include "stdafx.h"
#include "testA.h"
#include "testB.h"
void printBx()
{
    int nTmp = x;
    x = 5;
    printf("B.cpp::&x=[%p] 默认值x=[%d] x设置为5=[%d]\n", &x, nTmp, x);
    nTmp = y++;
    printf("B.cpp::&y=[%p] 默认值y=[%d] ++之后值y=[%d]\n", &y, nTmp, y);
}
int add(int a, int b)
{
    return a + b;
}main.cpp
#include "stdafx.h"
#include <windows.h>
#include "testA.h"
#include "testB.h"
int _tmain(int argc, _TCHAR* argv[])
{
    printAx();      // 打印A中变量X。cpp中已经修改
    printBx();      // 打印B中变量X(已修改)。cpp中已经修改
    printAx();      // 再次打印A中变量X。cpp中已经修改
    printf("main.cpp::&x=[%p] x=[%d] &y=[%p] y=[%d]\n", &x, x, &y, y);  // 再次打印A中变量X B中Y(默认)
    printf("main.cpp:: a+b=[%d]\n", add(x, y));
    /*
    * 结论
    * 1. static 变量作用范围为单个编译单元c/cpp
    * 2. 谨慎在头文件定义static全局成员,否则在后续其他模块包含该头文件中都会生产一个相同全局对象,而且都是相互独立互不干扰的
    * 3. 请在c/cpp中定义静态成员
    */
    system("pause");
	return 0;
}vs2013运行结果:

                










