在C+中何时使用Extern我正在阅读“在C+中思考”,它刚刚介绍了extern申报。例如:extern int x;extern float y;我想我理解这个意思(没有定义的声明),但我不知道它什么时候被证明是有用的。有人能举个例子吗?
3 回答
大话西游666
TA贡献1817条经验 获得超14个赞
extern int x;
int
x
x
x
static
标题:
#ifndef HEADER_H#define HEADER_H// any source file that includes this will be able to use "global_x"extern int global_x;void print_global_x();#endif
来源1:
#include "header.h"// it needs to be defined somewhereint global_x;int main(){ //set global_x here: global_x = 5; print_global_x();}
来源2:
#include <iostream>#include "header.h"void print_global_x(){ //print global_x here: std::cout << global_x << std::endl;}
呼如林
TA贡献1798条经验 获得超3个赞
int global_int = 1;
extern int global_int;//in some functioncout << "global_int = " << global_int;
梦里花落0921
TA贡献1772条经验 获得超6个赞
extern
.
extern
extern
const
const
来源1:
const int global = 255; //wrong way to make a definition of global const variable in C++
来源2:
extern const int global; //declaration
来源1:
extern const int global = 255; //a definition of global const variable in C++
来源2:
extern const int global; //declaration
- 3 回答
- 0 关注
- 422 浏览
添加回答
举报
0/150
提交
取消