看书上说nonconst variables are extern by default, but to make const variable accessible to other files we must explicitly specify that it is extern. 这么说的话如果我有两个文件: //file1.cpp int a=10; extern int b=11; void func(){ extern int c=12; } //file2.cpp #include int main(){ extern int a,b,c; std::cout<} 输出会是什么?谢谢!
woodbury outlet in NY. Beyondtherack.com, good wallet is sold out 5 minutes after its sales starts .
l*8
5 楼
编译不过吧,不能有两个main
variable
【在 s****A 的大作中提到】 : 看书上说nonconst variables are extern by default, but to make const variable : accessible to other files we must explicitly specify that it is extern. : 这么说的话如果我有两个文件: : //file1.cpp : int a=10; : extern int b=11; : void func(){ : extern int c=12; : } : //file2.cpp
谢谢 如果在file2.cpp里main函数外再加一个 int a; 这个a会受file1.cpp里的a的值影响吗?
【在 A**u 的大作中提到】 : a=10 : b= 11 : c没有定义
S*I
8 楼
extern means "defined somewhere else", so your file1.cpp won't compile. Talking about output from file2.cpp is meaningless.
variable
【在 s****A 的大作中提到】 : 看书上说nonconst variables are extern by default, but to make const variable : accessible to other files we must explicitly specify that it is extern. : 这么说的话如果我有两个文件: : //file1.cpp : int a=10; : extern int b=11; : void func(){ : extern int c=12; : } : //file2.cpp
s*A
9 楼
谢谢,其实我主要是不理解书上的"non const variables are extern by default."这 句话,要是如此的话,non const variable前面加不加extern不都一样吗?总是相当于 有extern
【在 S**I 的大作中提到】 : extern means "defined somewhere else", so your file1.cpp won't compile. : Talking about output from file2.cpp is meaningless. : : variable
S*I
10 楼
variables can be declared multiple times but can only be defined once. You can have "extern int a" in multiple files, but you can have only one "int a = 10" in all files.
【在 s****A 的大作中提到】 : 谢谢,其实我主要是不理解书上的"non const variables are extern by default."这 : 句话,要是如此的话,non const variable前面加不加extern不都一样吗?总是相当于 : 有extern