Mixing extern and const(混合外部和常量)
问题描述
我可以像 extern const 那样混合 extern 和 const 吗?如果是,const 限定符是否仅在其声明的范围内强加其统治,还是应该与其声明的翻译单元的声明完全匹配?IE.我可以声明 extern const int i;
即使实际 i 不是常量,反之亦然?
Can I mix extern and const, as extern const? If yes, does the const qualifier impose it's reign only within the scope it's declared in or should it exactly match the declaration of the translational unit it's declared in? I.e. can I declare say extern const int i;
even when the actual i is not a const and vice versa?
推荐答案
- 是的,您可以同时使用它们.
- 是的,它应该与实际声明的翻译单元中的声明完全匹配.当然,除非您参与了 Underhanded C 编程竞赛 :-)
- file.h:
extern const int a_global_var;
- file.c:
#include "file.h"
const int a_global_var =/* 一些常量表达式 */;
通常的模式是:
合并了legends2k的评论.谢谢.
Incorporated legends2k's comment. Thanks.
这篇关于混合外部和常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:混合外部和常量


基础教程推荐
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 我有静态或动态 boost 库吗? 2021-01-01