如何从另一个.cpp文件访问全局结构
How to have access to global struct from another .cpp file?
in Surface.h我有:
struct Surface{
bool isAllowedOnTile[TILETYPE_COUNT];
float moveBecomes; // When this is 0, it is ignored
float moveChange; // Is ignored if moveBecomes is non-zero
float affChange[ELEMENT_COUNT];
ID2D1BitmapBrush* pBrush;
};
在某些地方,我需要像这样初始化多个曲面:
Surface surface[SURFACEBMP_COUNT];
surface[0].moveBecomes = 123;
surface[0].moveChange = 0;
surface[0].affChange[0]= 2.0f;
...
然后我想要访问曲面[0],曲面[1],曲面[2]…从我的程序中的任何地方。我怎么做呢?
使用extern
,并使surface
全局
file.h
#ifndef FILE_H
#define FILE_H
...
extern Surface surface[SURFACEBMP_COUNT];
#endif
这是头文件,您应该在需要surface
的地方包含它。
file.cpp
#include "file.h"
Surface surface[SURFACEBMP_COUNT];
在头文件中使用
最简单的方法extern Surface surface[SURFACEBMP_COUNT];
然后在.cpp文件中声明并初始化它,并随时使用
相关文章:
- 如何使用 c++ 中的函数访问全局变量?
- 如何全局创建对象并使用该对象访问全局范围内的公共成员函数
- 访问全局数据结构
- 访问全局或静态对象的分离线程
- 可以静态访问全局静态变量的静态函数
- C++中是否可以通过非模板函数访问全局模板变量
- 如何在可执行文件中从.so访问全局变量
- C++:为什么与访问全局变量相比,访问类数据成员如此缓慢
- C++ 如何从全局对象中的成员对象访问全局对象
- 访问全局静态变量
- C++-访问全局变量对象
- 访问全局变量以修改值
- 如何访问全局类实例
- 使用PThreads访问全局列表-指针问题
- 如何从另一个.cpp文件访问全局结构
- 无法从 Visual C++ 窗体应用程序中的窗体访问全局变量
- 访问全局数组比作为参数传递更有效
- 声明和访问全局对象
- c++从类头文件中访问全局整数
- 类不能访问全局对象