分类: LINUX
2008-04-08 16:11:02
//const1.cpp
#include <iostream>
using namespace std;
const int a=1;
void funcA()
...{
cout<<"cout int A in file const2.cpp have value of "<<a<<endl;
}
// const2.cpp
#include <iostream>
using namespace std;
const int a=2;
extern void funcA();
void funcB()

...{
cout<<"cout int A in file const2.cpp have value of "<<a<<endl;
}
int main()
...{
funcA();
funcB();
}
g++ const1.cpp const2.cpp
./a.out
cout int A in file const2.cpp have value of 1
cout int A in file const2.cpp have value of 2chinaunix网友2009-03-10 20:48:54
对于C++,因为const int 是internal linkage,所以a可以在两个不同的cpp文件中定义。 C默认是external linkage