C++头文件中常量和静态常量有什么区别?

tom555cat 2013-12-04 08:16:54
在头文件里
----------------
const int AAA = 1;
----------------
static const int AAA = 1;
----------------
#ifndef M_AAA
const int AAA = 1;
#endif
----------------
这三个有什么区别?
...全文
579 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tom555cat 2013-12-05
  • 打赏
  • 举报
回复
引用 1 楼 davidsu33 的回复:
1. const int AAA = 1; 说明是常量,外部文件可以extern访问 2.static const int AAA = 1; 仅本文件内部可以访问 3. #ifndef M_AAA const int AAA = 1; #endif 根据宏的定义与否,与1的情况完全一样。
const对象默认为文件的局部变量,那么是不是static const和const就没什么区别了?
tom555cat 2013-12-05
  • 打赏
  • 举报
回复
引用 6 楼 mougaidong 的回复:
致1、2楼: 据在下所知,const 变量是内部链接的,真的可以extern访问吗? 致3楼: const只是个编译时告诉编译器进行严格检查的关键字,运行时没有任何约束力。 [quote=引用 1 楼 davidsu33 的回复:] 1. const int AAA = 1; 说明是常量,外部文件可以extern访问 2.static const int AAA = 1; 仅本文件内部可以访问 3. #ifndef M_AAA const int AAA = 1; #endif 根据宏的定义与否,与1的情况完全一样。
引用 2 楼 Sharing_Li 的回复:
[quote=引用 1 楼 davidsu33 的回复:] 1. const int AAA = 1; 说明是常量,外部文件可以extern访问 2.static const int AAA = 1; 仅本文件内部可以访问 3. #ifndef M_AAA const int AAA = 1; #endif 根据宏的定义与否,与1的情况完全一样。
+1[/quote]
引用 3 楼 zzbutcher 的回复:
静态变量 这种方式定义的变量能被本文件内的任何函数使用。其他文件中的函数不可使用。 常量是定以后,在程序运行中不能被改变 #ifndef M_AAA const int AAA = 1; #endif #ifndef M_AAA 用来测试M_AAA有没有被宏定义过,如果没有则对#ifndef和#endif之间的代码进行编译
[/quote] 如果将const对象定义为extern const,则在其他文件中也能引用该对象。
tom555cat 2013-12-05
  • 打赏
  • 举报
回复
引用 4 楼 max_min_ 的回复:
基础知识吧!可以google下 http://download.csdn.net/detail/max_min_/6333663补补基础!
就是想知道这三个什么情况下用,这个是书上没写的。
turing-complete 2013-12-05
  • 打赏
  • 举报
回复
你试过了?
引用 12 楼 davidsu33 的回复:
[quote=引用 11 楼 tom555cat 的回复:] [quote=引用 10 楼 davidsu33 的回复:] [quote=引用 9 楼 tom555cat 的回复:] [quote=引用 1 楼 davidsu33 的回复:] 1. const int AAA = 1; 说明是常量,外部文件可以extern访问 2.static const int AAA = 1; 仅本文件内部可以访问 3. #ifndef M_AAA const int AAA = 1; #endif 根据宏的定义与否,与1的情况完全一样。
const对象默认为文件的局部变量,那么是不是static const和const就没什么区别了?[/quote] 我已经说了,static和没有static的区别在于是否是本cpp文件内部访问,如果没有static,那么我们可以在头文件以extern const int AAA;的声明来使其他的CPP文件来进行访问的。[/quote] 那么没有static,也没有extern,就是一个const变量,它也只能在cpp文件内部访问啊。[/quote] 没有static是可以由外部cpp文件访问的[/quote]
davidsu33 2013-12-05
  • 打赏
  • 举报
回复
引用 11 楼 tom555cat 的回复:
[quote=引用 10 楼 davidsu33 的回复:] [quote=引用 9 楼 tom555cat 的回复:] [quote=引用 1 楼 davidsu33 的回复:] 1. const int AAA = 1; 说明是常量,外部文件可以extern访问 2.static const int AAA = 1; 仅本文件内部可以访问 3. #ifndef M_AAA const int AAA = 1; #endif 根据宏的定义与否,与1的情况完全一样。
const对象默认为文件的局部变量,那么是不是static const和const就没什么区别了?[/quote] 我已经说了,static和没有static的区别在于是否是本cpp文件内部访问,如果没有static,那么我们可以在头文件以extern const int AAA;的声明来使其他的CPP文件来进行访问的。[/quote] 那么没有static,也没有extern,就是一个const变量,它也只能在cpp文件内部访问啊。[/quote] 没有static是可以由外部cpp文件访问的
tom555cat 2013-12-05
  • 打赏
  • 举报
回复
引用 10 楼 davidsu33 的回复:
[quote=引用 9 楼 tom555cat 的回复:] [quote=引用 1 楼 davidsu33 的回复:] 1. const int AAA = 1; 说明是常量,外部文件可以extern访问 2.static const int AAA = 1; 仅本文件内部可以访问 3. #ifndef M_AAA const int AAA = 1; #endif 根据宏的定义与否,与1的情况完全一样。
const对象默认为文件的局部变量,那么是不是static const和const就没什么区别了?[/quote] 我已经说了,static和没有static的区别在于是否是本cpp文件内部访问,如果没有static,那么我们可以在头文件以extern const int AAA;的声明来使其他的CPP文件来进行访问的。[/quote] 那么没有static,也没有extern,就是一个const变量,它也只能在cpp文件内部访问啊。
davidsu33 2013-12-05
  • 打赏
  • 举报
回复
引用 9 楼 tom555cat 的回复:
[quote=引用 1 楼 davidsu33 的回复:] 1. const int AAA = 1; 说明是常量,外部文件可以extern访问 2.static const int AAA = 1; 仅本文件内部可以访问 3. #ifndef M_AAA const int AAA = 1; #endif 根据宏的定义与否,与1的情况完全一样。
const对象默认为文件的局部变量,那么是不是static const和const就没什么区别了?[/quote] 我已经说了,static和没有static的区别在于是否是本cpp文件内部访问,如果没有static,那么我们可以在头文件以extern const int AAA;的声明来使其他的CPP文件来进行访问的。
turing-complete 2013-12-04
  • 打赏
  • 举报
回复
致1、2楼: 据在下所知,const 变量是内部链接的,真的可以extern访问吗? 致3楼: const只是个编译时告诉编译器进行严格检查的关键字,运行时没有任何约束力。
引用 1 楼 davidsu33 的回复:
1. const int AAA = 1; 说明是常量,外部文件可以extern访问 2.static const int AAA = 1; 仅本文件内部可以访问 3. #ifndef M_AAA const int AAA = 1; #endif 根据宏的定义与否,与1的情况完全一样。
引用 2 楼 Sharing_Li 的回复:
[quote=引用 1 楼 davidsu33 的回复:] 1. const int AAA = 1; 说明是常量,外部文件可以extern访问 2.static const int AAA = 1; 仅本文件内部可以访问 3. #ifndef M_AAA const int AAA = 1; #endif 根据宏的定义与否,与1的情况完全一样。
+1[/quote]
引用 3 楼 zzbutcher 的回复:
静态变量 这种方式定义的变量能被本文件内的任何函数使用。其他文件中的函数不可使用。 常量是定以后,在程序运行中不能被改变 #ifndef M_AAA const int AAA = 1; #endif #ifndef M_AAA 用来测试M_AAA有没有被宏定义过,如果没有则对#ifndef和#endif之间的代码进行编译
SKATE11 2013-12-04
  • 打赏
  • 举报
回复
#ifndef M_AAA const int AAA = 1; #endif 这样做可防止头文件重复引用问题
max_min_ 2013-12-04
  • 打赏
  • 举报
回复
基础知识吧!可以google下 http://download.csdn.net/detail/max_min_/6333663补补基础!
zzbutcher 2013-12-04
  • 打赏
  • 举报
回复
静态变量 这种方式定义的变量能被本文件内的任何函数使用。其他文件中的函数不可使用。 常量是定以后,在程序运行中不能被改变 #ifndef M_AAA const int AAA = 1; #endif #ifndef M_AAA 用来测试M_AAA有没有被宏定义过,如果没有则对#ifndef和#endif之间的代码进行编译
Sharing_Li 2013-12-04
  • 打赏
  • 举报
回复
引用 1 楼 davidsu33 的回复:
1. const int AAA = 1; 说明是常量,外部文件可以extern访问 2.static const int AAA = 1; 仅本文件内部可以访问 3. #ifndef M_AAA const int AAA = 1; #endif 根据宏的定义与否,与1的情况完全一样。
+1
davidsu33 2013-12-04
  • 打赏
  • 举报
回复
1. const int AAA = 1; 说明是常量,外部文件可以extern访问 2.static const int AAA = 1; 仅本文件内部可以访问 3. #ifndef M_AAA const int AAA = 1; #endif 根据宏的定义与否,与1的情况完全一样。

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧