在 C++ 中创建一个常量数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6036453/
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
making a constant array in c++
提问by hotdiggadydang
Is there any reason why codeblocks is telling me that I can't make an array? I'm simply trying to do:
代码块告诉我我不能创建数组有什么原因吗?我只是想做:
const unsigned int ARRAY[10] = {0,1,2,3,4,5,6,7,8,9};
and it's giving me
它给了我
error: a brace-enclosed initializer is not allowed here before '{' token
错误:此处不允许在“{”标记之前使用大括号括起来的初始值设定项
I have changed other parts of the initializer, but the error is always saying the same thing. This doesn't seem to make sense, since this is one of the first things I learned in c++.
我已经更改了初始化程序的其他部分,但错误总是说同样的事情。这似乎没有意义,因为这是我在 C++ 中学到的第一件事。
回答by Lightness Races in Orbit
You say that you did this within a class, as a private variable.
你说你在一个类中做了这个,作为一个私有变量。
Recall that (at the moment), member variables may not be initialisedin the same place where you declare them (with a few exceptions).
回想一下(目前),成员变量可能不会在您声明它们的同一个地方初始化(有一些例外)。
struct T {
std::string str = "lol";
};
is not ok. It has to be:
不行。它一定要是:
struct T {
std::string str;
T() : str("lol") {}
};
But, to add insult to injury, pre-C++0x you cannot initialise arrays in the ctor-initializer
!:
但是,雪上加霜的是,在 C++0x 之前,您不能在ctor-initializer
! 中初始化数组:
struct T {
const unsigned int array[10];
T() : array({0,1,2,3,4,5,6,7,8,9}) {} // not possible :(
};
And, because your array's elements are const
, you can't rely on assignment either:
而且,因为您的数组元素是const
,所以您也不能依赖赋值:
struct T {
const unsigned int array[10];
T() {
for (int i = 0; i < 10; i++)
array[i] = i; // not possible :(
}
};
However, as some other contributors have quite rightly pointed out, there seems little point in having a copy of the array for each instance of T
if you can't modify its elements. Instead, you could use a static
member.
但是,正如其他一些贡献者非常正确地指出的那样,T
如果您无法修改其元素,则为每个实例拥有一个数组副本似乎没有什么意义。相反,您可以使用static
成员。
So, the following will ultimately solve your problem in what's — probably — the best way:
因此,以下内容最终将以最好的方式解决您的问题:
struct T {
static const unsigned int array[10];
};
const unsigned int T::array[10] = {0,1,2,3,4,5,6,7,8,9};
Hope this helps.
希望这可以帮助。
回答by Cubbi
Since this is a private member variable in a class (according to the comment), this is indeed not allowed in C++03.
由于这是类中的私有成员变量(根据注释),这在 C++03 中确实是不允许的。
C++0x, partially supported by many modern compilers, allows the following to compile:
许多现代编译器部分支持的 C++0x 允许编译以下内容:
class C
{
const unsigned int ARRAY[10];
public:
C() : ARRAY{0,1,2,3,4,5,6,7,8,9} {}
};
int main()
{
C obj; // contains a non-static const member: non-assignable
}
However, non-static const members only make sense if they contain differentvalues in different instances of the class. If every instance is to contain the same {0,1,2,3,4,5,6,7,8,9}
, then you should make it static
, which also makes it possible to do this in C++98:
然而,非静态常量成员只有在类的不同实例中包含不同的值时才有意义。如果每个实例都包含相同的{0,1,2,3,4,5,6,7,8,9}
,那么你应该 make it static
,这也使得在 C++98 中做到这一点成为可能:
class C
{
static const unsigned int ARRAY[10];
public:
C() {}
};
const unsigned int C::ARRAY[10] = {0,1,2,3,4,5,6,7,8,9};
int main()
{
C obj;
}