C++ - 常量变量,这是一个正确的术语吗?
在本文的末尾: http://www.learncpp.com/ cpp-tutorial/45-enumerated-types/
它提到了下面这句话:
最后,与常量变量一样,枚举类型出现在调试器中,使它们比#在这方面定义了值。
“常量变量”是一个正确的术语吗?常量和变量不一样吗?
谢谢。
At the end of the artile here: http://www.learncpp.com/cpp-tutorial/45-enumerated-types/
It mentions the following sentence:
Finally, as with constant variables, enumerated types show up in the debugger, making them more useful than #defined values in this regard.
Is "constant variables" a correct term to use? Aren't constants different from variables?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从表面上看,这是一个明显的矛盾,并且可能是由于历史原因而产生的(“变量”用于指代与名称和类型相关的内存位置)。
不过,我可以尝试证明这个词的合理性:
在编译时,它是一个变量 - 在优化之前,它具有变量所具有的所有属性 - 占用内存,具有位置等等 - 并且处理方式大致相同。 (相比之下,您提供的值(例如 43423L、3.141 或文字“somestring”)不是变量。)
在运行时,它不能再改变,并且被视为常量。它有点像“一次写入变量”,您可以在源代码中设置它。
It is an obvious contradiction in terms on the surface, and has probably come about for historical reasons ("variable" being used to refer to a memory location associated with a name and a type).
However, I can try to justify the term:
At compile time, it is an variable - before optimization, it has all the properties a variable has - takes up memory, has a location and so on - and is handled much the same. (In contrast, an value you provide like 43423L, 3.141 or a literal "somestring" is not a variable.)
At runtime, it cannot be changed anymore, and is seen as constant. It's sort of a "write-once variable", which you set in the source.
很多人都很困惑。在 K&RC 中,可以更改字符串文字。因此,文字是常量意味着不可变是错误的。在 ISO C 和 C++ 中,某些 T 的 T 类型 const 变量也可以使用强制转换来更改。再说一遍,恒定并不意味着一成不变。正如@foo上面指出的,由于优化,常量通常不具有变量的属性,事实上语义经常指定它。另外,不要忘记在 C 和 C++ 中存在常量表达式,有时称为编译时常量,用于数组边界,例如,一种永远不会与变量混淆的常量,同意吗?
所以这里有一个定义:常量是名称与值的绑定。根据此定义,
1
是一个常量,因为它是一个文字名称,并且绑定是隐式的。这里有两个常量,即
x
和f
:它们是符号名称的绑定(在本例中两个标识符,在 C++ 中operator+
是也是名称但不是标识符)。现在,我将向您展示另一个令您惊讶的常数!
是的,确实,
y
也是一个常量!它是名称y
到地址的绑定。[如果在堆栈上,则为相对于框架基数的偏移量]
因此,如果您认真思考的话,实际上抽象语法中的所有内容都是常量:)
So many people are confused. In K&R C, a string literal can be changed. Hence, literals being constants meaning immutable is wrong. In ISO C and C++ variables of type T const for some T can also be changed, using a cast. So again, constant doesn't mean immutable. As @foo pointed out above, constants often do not have the properties of a variable because of optimisation, and in fact the semantics often specify it. Also do not forget in C and C++ there are constant expressions sometimes known as compile-time constants, used for array bounds, for example, a kind of constant which would never be confused with a variable, agree?
So here's a definition: a constant is a binding of a name to a value. With this definition,
1
is a constant because it is a literal name, and the binding is implicit. And herethere are two constants, namely
x
andf
: these are bindings of symbol names (in this case both identifiers, in C++operator+
is also a name but not an identifier).Now, I will show you another constant which will surprise you!
Yes indeed,
y
is a constant too! It is a binding of the namey
to an address.[If on the stack, an offset from the frame base instead]
So actually pretty much everything in your abstract syntax is a constant if you think about it hard enough :)
它是一个变量,因为它是一个可寻址的对象,而不是一个文字常量,后者是不可寻址的。
这可能是矛盾的,但语言标准使用术语“变量”来指代一般可寻址对象,并使用“const”来指定“只读”可寻址对象。
It is a variable in the sense that it is an addressable object, as opposed to a literal constant which is not addressable.
It is perhaps contradictory, but the language standard uses the term variable to refer to addressable objects in general, and
const
to specify a read-only addressable object.C++ 中的常量是一种特殊的变量。我知道这听起来可能与常识相矛盾,但 C++ 就是这样。
它们之间的区别仅在于
const
关键字,它告诉编译器var2
的值一旦创建就不得更改:这个简单的规则也导致了进一步的约束对于
const
变量强制执行,例如,您不能创建对const
变量的非const
引用(否则您仍然可以更改其通过该参考值)。出于同样的原因,您也不允许将var2
传递给具有签名f(int& i)
的函数。但是,您可以创建对它们的 const 引用、使用它们的值等,就像任何“普通”变量一样。所以总的来说,它们就像任何其他变量一样。Constants in C++ are a special kind of variable. I understand it may sound contradictory for the common sense, but that's how it is in C++.
The difference between them is only the
const
keyword, which tells the compiler that the value ofvar2
must not be changed once it is created:This simple rule also causes further constraints to be enforced for
const
variables, e.g. you can't create a non-const
reference to aconst
variable (as otherwise you could still change its value via that reference). Neither you are allowed to passvar2
to a function with a signaturef(int& i)
, for the same reason. However, you can createconst
references to them, use their values etc. just as with any "normal" variable. So by and large they are like any other variable.是的,这在术语上有点矛盾。变量是根据定义而变化的事物,常量是根据定义而变化的事物。
除此之外,在 C++(和其他语言)中,人们仍然称它们为常量变量,因为我们本质上将任何具有名称的值视为变量。
不过,请随意说“恒定”。
Yes, it's a bit of a contradiction in terms. Variables are things that vary by definition, and constants are things that don't vary by definition.
That aside, in C++ (and other languages), people still call them constant variables because we essentially think of any value with a name as a variable.
Feel free to just say 'constant' though.
常量就是变量。在 C++(和大多数语言)中,将变量定义为常量只是告诉编译器不允许更改该变量。
A constant is a variable. In C++ (and most languages), defining a variable as a constant simply tells the compiler that the variable is not allowed to be changed.