const关键字的internal linkage属性

来源:互联网 发布:傲剑肉身升级数据 编辑:程序博客网 时间:2024/06/03 14:50

     当将const关键字用于声明某个常量时,该标识符自动具备internal linkage属性,即只对相同文件内的函数可见,对其他文件中的函数是不可见的。这可以通过如下的示例程序证明

   
//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 2

原创粉丝点击