如何在 C++ 对象中初始化数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10694689/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:21:17  来源:igfitidea点击:

How to initialize an array in C++ objects

c++arraysclassinitialization

提问by Quade2002

After reading How to initialize an array in C, in particular:

阅读如何在 C 中初始化数组后,特别是:

Don't overlook the obvious solution, though:

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

但是,不要忽视明显的解决方案:

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

I tried something like this:

我试过这样的事情:

#include <iostream>

class Something {
private:

int myArray[10];

public:

Something() {
    myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
}

int ShowThingy(int what) {
    return myArray[what];
}

~Something() {}
};

int main () {
   Something Thing;
    std::cerr << Thing.ShowThingy(3);
}

And I get:

我得到:

..\src\Something.cpp: In constructor 'Something::Something()':
..\src\Something.cpp:10:48: error: cannot convert '<brace-enclosed initializer list>' to 'int' in assignment

The obvious in this case is not so obvious. I really would like the initiation of my array to be more dynamic as well.

在这种情况下,显而易见的事情并不那么明显。我真的希望我的数组的启动也更加动态。

I tired:

我累了:

private:
    int * myArray;

public:
    Something() {
            myArray = new int [10];
            myArray = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
}

This looked funky to me to, and so to the compiler:

这对我来说看起来很时髦,对编译器来说也是如此:

..\src\Something.cpp: In constructor 'Something::Something()':
..\src\Something.cpp:11:44: error: cannot convert '<brace-enclosed initializer list>' to 'int*' in assignment

This also did not work:

这也不起作用:

private:
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

with:

和:

 ..\src\Something.cpp:6:20: error: a brace-enclosed initializer is not allowed here before '{' token
 ..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers
 ..\src\Something.cpp:6:51: error: 'constexpr' needed for in-class initialization of static data member 'myArray' of non-integral type

I have been doing really good and learning what does not work, but not so good learning what does work.

我一直做得很好,学习什么不起作用,但不太好学习什么起作用。

So, how do I used initialization lists {value, value, value} for an array inside a class?

那么,我如何为类中的数组使用初始化列表 {value, value, value} ?

I have been trying to figure out how to do this for some time now and am very stuck, I have a number of these kinds of lists I need to make for my app.

一段时间以来,我一直试图弄清楚如何做到这一点,但我很困惑,我需要为我的应用程序制作许多此类列表。

回答by Praetorian

You need to initialize the array in the constructor initialization list

需要在构造函数初始化列表中初始化数组

#include <iostream>

class Something {
private:

int myArray[10];

public:

Something()
: myArray { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }
{
}

int ShowThingy(int what) {
    return myArray[what];
}

~Something() {}
};

int main () {
   Something Thing;
    std::cerr << Thing.ShowThingy(3);
}

..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers

..\src\Something.cpp:6:51: 抱歉,未实现:非静态数据成员初始值设定项

C++11 also adds supports for inline initialization of non-static member variables, but as the above error message states, your compiler has not implemented this yet.

C++11 还添加了对非静态成员变量的内联初始化的支持,但正如上面的错误消息所述,您的编译器尚未实现这一点。

回答by derekerdmann

Unless I'm mistaken, the initializer list is only allowed for when the variable is initialized during declaration - hence the name. You can't assign an initializer list to a variable, as you're trying to do in most of your examples.

除非我弄错了,只有在声明期间初始化变量时才允许使用初始化器列表 - 因此得名。您不能像在大多数示例中那样将初始化列表分配给变量。

In your last example, you're trying to add static initialization to a non-static member. If you want the array to be a static member of the class, you could try something like this:

在上一个示例中,您尝试向非静态成员添加静态初始化。如果你想让数组成为类的静态成员,你可以尝试这样的事情:

class Derp {
private:
    static int myArray[10];
}

Derp::myArray[] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

If you want to add a class member, you could try making the static array constand copy it into the member array in the constructor.

如果要添加类成员,可以尝试制作静态数组const并将其复制到构造函数中的成员数组中。