glovar.h 文件
#pragma once
static int glo;
one.h 文件
void getone();one.cpp 文件#include "stdafx.h"
#include "glovar.h"
#include "one.h"
void getone()
{
glo = 13;
printf("one: glo is %d \n",glo);
}
two.h 文件
void gettwo();
two.cpp 文件
#include "stdafx.h"
#include "glovar.h"
#include "one.h"
#include "two.h"
void gettwo()
{
glo = 15;
getone();
printf("two: glo is %d \n",glo);
}main.cpp 文件
#include "stdafx.h"
#include "one.h"
#include "two.h"
int _tmain(int argc, _TCHAR* argv[])
{
gettwo();
return 0;
}运行结果:
one: glo is 13
two: glo is 15

本文详细介绍了C++程序中全局变量的定义与使用方法,重点探讨了如何在不同文件间引用全局变量,并通过具体实例展示了全局变量在多文件结构中的作用与影响。
123

被折叠的 条评论
为什么被折叠?



