/*
**test14.cpp : Defines the entry point for the console application.
**系统winXP SP3 32位.
**外连接、内连接、无连接
*/
#include "stdafx.h"
#include "stdio.h"
extern const int extern_iNum = 0; //该常量是外部连接
int main(int argc, char* argv[])
{
void fExternal();
int iNum = 0; //全局变量是外部连接
const int const_iNum = 0; //该常量是内部连接
return 0;
}
void fExternal(){ //函数是外部连接
int inner_Num; //无连接,只能在fExternal函数内部调用
printf("function is out join");
inner_Num = 0; //注意这里如果不加上这句话,有的编译环境会出现警告或者错误,本环境下报错unreferenced local variable
}
namespace NS{
int iNum; //NS::iNum是外连接的
void function1(){}; //NS::function1是外连接的
}
static void sfExtenal(){ //函数是内部连接
printf("function is inner join");
}
Tips1:我们知道C语言为变量、常量、函数等定义了四种存储类型:extern(永久)、static(永久)、auto(临时)、register(临时)。其在程序中的保存期限分为永久(整个程序开始到消亡)、与临时(暂时存储在内存堆栈中)。
Tips2:显式声明为static的全局变量、函数,只能被同一个编译单元内访问。全局常量想要被外部访问需要加extern声明。
Tips3:当我们定义一个相同名字的变量”int iNum = 0;”在file1.cpp与file2.cpp中时,分别编译不会出现错误,但是两个文件一起编译的时候会出现重命名错误,导致连接失败——原因就是他们都是外连接的。这时候只需要把其中的一个定义为static。