打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
const头文件定义问题的讨论
最近看c++ primer,上面这么说,const对象默认为文件的局部变量,这样就可以在头文件中定义而不会出现重复定义的错误了.
可是我在myheader.h里面写了:
const int CON=100;
在main.cpp里面写了
C/C++ code
#include<iostream>#include"myheader.h"using namespace std;int main(){cout << CON;
system("pause");return 0;}
可是运行却没有错.为什么??
既然是局部变量,即便其他cpp包含了.h,也应该不能使用吧,就像定义在函数作用域中的变量,即便被包含,也不能使用一样吧.
求解释,看过相关帖,没有明白的答案.
更准确地说,是编译单元的局部(可见)变量……
#include是简单的文本文本替换……
所以main.cpp是可以用的……
这里const的作用是……
如果另外有一个main1.cpp也#include"myheader.h",那么在main1.cpp里面也有一个CON,这个CON跟main.cpp里面的CON不是同一个,它们之间没有冲突,不会相互干扰……
如果CON前面不加const,那么如果有两个cpp同时#include"myheader.h",那么连接时就会出问题
const这个性质恰恰是为了“能用不冲突”而不是“看不见用不着”
一般的像函数,变量等,都是有外部链接的,他们在一个程序里,只能被定义一次,所以他们不能放头文件里定义,而在头文件中只能声明一下。
而const,按照默认约定,是内部连接,只有本编译单元里可以访问,所以可以放在头文件中,被所有编译单元包含。
C++中加了const就默认具有内部链接属性。
C中可谓有const还是外部链接属性。
除非用static,extern等显示地修饰。
那个extern,感觉既能表示外部连接属性,又能表示声明而不是定义?
就像extern int a; //声明
int a; //定义
extern const int a; //设置外链属性
这两个作用有什么联系吗?特别不明白为什么两个要用一个关键字
因为const变量默认就是内联属性的。用extern是将它声明为外链属性
#include<iostream>
你既然都#include"myheader.h"了,
那么代码就和以下的等效了:
using namespace std;
const int CON=100;
int main()
{
cout << CON;
system("pause");
return 0;
}
关于linkage
A name is said to have linkage when it might denote the same object, reference, function, type, template,
namespace or value as a name introduced by a declaration in another scope:
— When a name has external linkage, the entity it denotes can be referred to by names from scopes of
other translation units or from other scopes of the same translation unit.
— When a name has internal linkage, the entity it denotes can be referred to by names from other scopes in
the same translation unit.
— When a name has no linkage, the entity it denotes cannot be referred to by names from other scopes.
关于全局scope
The outermost declarative region of a translation unit is also a namespace, called the global namespace. A
name declared in the global namespace has global namespace scope (also called global scope). The potential
scope of such a name begins at its point of declaration (3.3.1) and ends at the end of the translation unit
that is its declarative region. Names with global namespace scope are said to be global.
关于namespace scope和linkage
A name having namespace scope (3.3.5) has internal linkage if it is the name of
— an object, reference, function or function template that is explicitly declared
— an object or reference that is explicitly declared const and neither explicitly declared extern nor
previously declared to have external linkage; or
— a data member of an anonymous union.
4 A name having namespace scope has external linkage if it is the name of
— an object or reference, unless it has internal linkage; or
— a function, unless it has internal linkage; or
— a named class (clause 9), or an unnamed class defined in a typedef declaration in which the class has the
typedef name for linkage purposes (7.1.3); or
— a named enumeration (7.2), or an unnamed enumeration defined in a typedef declaration in which the
enumeration has the typedef name for linkage purposes (7.1.3); or
— an enumerator belonging to an enumeration with external linkage; or
— a template, unless it is a function template that has internal linkage (clause 14); or
— a namespace (7.3), unless it is declared within an unnamed namespace.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
‘exit’ was not declared in this scope
standards
Internal and External Linkage in C++ – Peter Goldsborough
Check Lua source code for various errors.
C++之‘malloc’ was not declared in this scope和invalid conversion from ‘void*’ to ‘char*’
C/C++中extern的作用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服