在编译多文件时,我们常常需要在一个文件中引用其他文件的外部变量(var)。变量有如下定义:
statu 1:
type var = type();
statu 3:
const type var = type();
如果显示提供存储类(static/extern),又将会有另两中情况。
我们知道。
static type var=xxx;将使var具有内部连接,即文件作用域,其他文件将无法看到并使用这个var.
statu 1经过实验,其隐含外部连接。extern type var,将使var具有外部作用。
statu 2经过实验,其隐含内部连接。extern const type var, 将使得var出现 "undefined referenced to var"连接错误。
如果在上面两中情况下明确指明外部连接情况,即:
extern type var1 = xxx;
extern const type var2 = xxx;
当在其他文件中声明/引用
extern type var1;
extern const type var2;
时,statuc 1出现一个警告,statu 2顺利通过。
所以,对于const type var2;我们还是明确将其定义成static const type var吧;否则定义成extern const type var。
明确指明它具有外部连接还是内部连接。
C++中毕竟还是准确的好。
statu 1:
type var = type();
statu 3:
const type var = type();
如果显示提供存储类(static/extern),又将会有另两中情况。
我们知道。
static type var=xxx;将使var具有内部连接,即文件作用域,其他文件将无法看到并使用这个var.
statu 1经过实验,其隐含外部连接。extern type var,将使var具有外部作用。
statu 2经过实验,其隐含内部连接。extern const type var, 将使得var出现 "undefined referenced to var"连接错误。
如果在上面两中情况下明确指明外部连接情况,即:
extern type var1 = xxx;
extern const type var2 = xxx;
当在其他文件中声明/引用
extern type var1;
extern const type var2;
时,statuc 1出现一个警告,statu 2顺利通过。
所以,对于const type var2;我们还是明确将其定义成static const type var吧;否则定义成extern const type var。
明确指明它具有外部连接还是内部连接。
C++中毕竟还是准确的好。