2.4-2

来源:互联网 发布:js运动小球重力 编辑:程序博客网 时间:2024/04/30 01:55

first_head.h
--------------------------------
#include<iostream>
using namespace std;

int a=10;//非const变量默认为extern
//const int a=10;//error:在全局作用域声明的const变量是定义该对象的文件的局部变量
                   此文件只存在于那个文件中,不能被其他文件访问
extern const int b=10;//指定const变量为extern,就可以在整个程序中访问const对象

first.cpp
--------------------------------
int main()
{
 extern int a;
 cout<<a<<endl; //非const变量定义在一个文件中,可在另一个文件中使用这个变量
 cout<<b<<endl;


  system("pause");
}

原创粉丝点击