C++ extern用法

来源:互联网 发布:快三遗漏数据 编辑:程序博客网 时间:2024/05/23 12:49

consoleapplication.cpp

#include "stdafx.h"#include<iostream>using namespace std;extern int n;extern int fun();void main() {cout << n << endl;cout << fun() << endl;}
program.cpp

#include "stdafx.h"#include<iostream>using namespace std;int n=88;int fun() {return n;}

实验现象:

88
88
请按任意键继续. . .


如上面代码所示,当我们在一个cpp文件中定义了一个变量或者一个函数,如果我们想要在其他文件中使用它们,我们只需要在变量或者函数前面加上extern声明,就可以使用了。

原创粉丝点击