C++ case 表达式不是常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8049834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Case expression not constant
提问by SirYakalot
I am getting a 'case expression not constant' error in a switch statement. However, the header provides a definition for the used constants, and the constructor provides initialisation for them in its initialization list.
我在 switch 语句中收到“case expression not constant”错误。但是,头文件为使用的常量提供了定义,构造函数在其初始化列表中为它们提供了初始化。
Additionally, when I mouse over the "problem" statements it identifies them as constants.
此外,当我将鼠标悬停在“问题”陈述上时,它会将它们标识为常量。
const int ThisClass::EXAMPLE_CONSTANT
error expression must have a constant value
This seems a little counter-intuitive to me. I did some research and found a similar problem that someone else had. They were told that all constants must in fact be initialised in 'main' and that this was a limitation of the language. Is this really the case? It seems unlikely.
这对我来说似乎有点违反直觉。我做了一些研究,发现了其他人遇到的类似问题。他们被告知实际上所有常量都必须在“main”中初始化,这是语言的限制。真的是这样吗?似乎不太可能。
采纳答案by Nawaz
The case
statements require integral value which must be known at compile-time, which is what is meant by constanthere. But the const
members of a classare not really constantin that sense. They're are simply read-only.
这些case
语句需要在编译时必须知道的整数值,这就是这里常量的含义。但是从这个意义上说,类的const
成员并不是真正不变的。它们只是只读的。
Instead of fields, you can use enum
:
您可以使用enum
以下字段代替字段:
class ThisClass
{
public:
enum Constants
{
EXAMPLE_CONSTANT = 10,
ANOTHER_CONSTANT = 20
};
};
And then you can write,
然后你可以写,
switch(c)
{
case ThisClass::EXAMPLE_CONSTANT:
//code
break;
case ThisClass::ANOTHER_CONSTANT:
//code
break;
};
回答by Sebastian Mach
You need a "real" compile time integer constant. const
in C++ means read-only, and a const variable can
be initialized just like int y = 0; const int x = y;
, making x
a read-only copy of the value y
had at the time of initialization.
您需要一个“真正的”编译时整数常量。const
在 C++ 中意味着只读,并且可以像 一样初始化 const 变量int y = 0; const int x = y;
,从而x
生成初始化时的值的只读副本y
。
With a modern compiler, you can either use enum
s or constexpr
s to store (integral) members of compile-time-constness:
使用现代编译器,您可以使用enum
s 或constexpr
s 来存储 compile-time-constness 的(整数)成员:
class Foo {
public:
static constexpr int x = 0;
enum { y = 1 };
};
int main () {
switch (0) {
case Foo::x: ;
case Foo::y: ;
}
}
回答by R. Martinho Fernandes
This is a bit of a mess. In C++ const
can be used for several things, like declaring actual constants, and declaring read-only variables.
这有点乱。在 C++const
中可用于多种用途,例如声明实际常量和声明只读变量。
If you declare:
如果您声明:
const int x = 0;
In global, namespace, or local scope, it is a constant. You can use it where constant expressions are required (like case labels or array sizes). However, at class scope or as a function parameter, it's just a read-only variable.
在全局、命名空间或局部范围内,它是一个常量。您可以在需要常量表达式的地方使用它(如案例标签或数组大小)。但是,在类范围内或作为函数参数,它只是一个只读变量。
Additionally, if you declare at class scope:
此外,如果您在类范围内声明:
static const int x = 0;
This is also a constant.
这也是一个常数。
回答by AnT
Constants used in case labels must be integral constant expressions. An integral constant expressionmust satisfy a much more strict set of requirements than just being an integral object declared as const
.
用于 case 标签的常量必须是整型常量表达式。一个积分常量表达式必须满足的一组比只是被声明为一个整体的对象的要求要严格得多const
。
A non-static class member cannot be used in an integral constant expression, so what you are trying to do will not compile. A static class member, for example, can be used in an integral constant expression if its initializer is "visible" at the point of use.
非静态类成员不能用于整型常量表达式,因此您尝试执行的操作将无法编译。例如,如果静态类成员的初始值设定项在使用时“可见”,则可以在整型常量表达式中使用它。