简体   繁体   English

在c ++中创建一个常量数组

[英]making a constant array in c++

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 ++中学到的第一件事。

You say that you did this within a class, as a private variable. 你说你在一个类中做了这个,作为一个私有变量。

Recall that (at the moment), member variables may not be initialised in 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. 但是,正如其他一些贡献者已经非常正确地指出的那样,如果你不能修改它的元素,那么为每个T实例提供一个数组副本似乎没什么意义。 Instead, you could use a static member. 相反,您可以使用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. 希望这可以帮助。

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 different values in different instances of the class. 但是,非静态const成员只有在类的不同实例中包含不同的值时才有意义。 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} ,那么你应该将它设置为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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM