楼主:
zzss2003 (brotherD)
2017-10-26 15:05:34小弟最近在看很多sepc,注意到constant这个字好像跟我心目中的定义不太一样。
我一直以为 3, 0x2A, 0b10001000这些就是所谓的constant
但看到这份文件: https://en.wikipedia.org/wiki/Constant_(computer_programming)#Comparison_with_literals_and_macros
Definition: a constant is a value that cannot be altered by the program during
normal execution
这段给我的感觉是,只要在normal exectuion的过程中,值不被改变就叫constant
问题一:什么是normal execution?
接着文件又说: When associated with an identifier, a constant is said to be
"named," although the terms "constant" and "named constant" are often used
interchangeably.
这段告诉我的意思是,以下程式码:
int a = 3;
只要a的值在normal execution过程中没有被改变,那a就叫 named constant,并不叫
variable.
请问各位大大,我理解的方向是正确的吗?
作者:
djshen (djshen)
2017-10-26 16:24:00literal?
作者:
jimfan (jimfan)
2017-10-26 17:43:003,0x2A系constant不错呀,而int a = 3; 将3这个数值赋予给**变量** a,a的初始值是3,不过是可以改变的所以a依然系variable,至少编译器不会阻止 a = 999;
作者: AstralBrain 2017-10-26 18:34:00
看你在看什么语言的spec, 一般人认为的constant可能会跟spec定义的不太一样例如说在c spec里面, const int a = 3;的 a 不是constant写 enum {X = a}, compiler会对这个a有意见但是在一般人的定义里面a是个constant
作者:
Lipraxde (Lipraxde)
2017-10-26 19:19:00我是当作int,有const的特性@@
作者:
kingofsdtw (ä¸èƒ½é–’下來!!)
2017-10-27 00:16:00你把一个int vale丢到 func(const int &val,...)你可以很确认的知道value不会被改变或是你从 const data_t func(...)获得资料你可以知道你获的的data是无法改变的但是可以 看 or copy
作者:
PkmX (阿猫)
2017-10-27 00:51:00楼上return const value是没有意义的
作者: SaberZ7 (Sean) 2017-10-27 01:07:00
先看看C规格书怎么写