64,685
社区成员
发帖
与我相关
我的任务
分享
//t.h
class M
{
public:
static int b;
};
static int a;
//t1.cpp
#include<iostream>
#include"t.h"
using namespace std;
int main()
{
cout<<a<<endl;
cout<<M::b<<endl;
}
//t2.cpp
#include"t.h"
extern int a=1;
int M::b=2;
运行结果:
0
2
/*
1、
这说明static int a的作用域是在这个文件内,所有t2中赋值1跟t1中没有关系,不是同一个变量
那为什么在类中就是同一个变量了呢?
static已经限制在一个文件中了
//////////////////////
2、
在t2.cpp中,必须写成extern int a=1;
为什么不能写成a=1?
他已经包含头文件了
*/