C++ 如何在构造函数中初始化类的成员数组?

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

How to initialise a member array of class in the constructor?

c++

提问by chintan s

I am trying to do the following:

我正在尝试执行以下操作:

class sig
{
public:

int p_list[4];
}

sig :: sig()
{
p_list[4] = {A, B, C, D};
}

I get an error

我收到一个错误

missing expression in the constructor.

构造函数中缺少表达式。

So how do I initilalise an array?

那么如何初始化一个数组呢?

回答by Kerrek SB

In C++11 only:

仅在 C++11 中:

class sig
{
    int p_list[4];
    sig() : p_list { 1, 2, 3, 4 }   {   }
};

Pre-11 it was not possible to initialize arrays other than automatic and static ones at block scope or static ones at namespace scope.

在 11 之前,除了块范围内的自动和静态数组或命名空间范围内的静态数组之外,不可能初始化数组。

回答by Konrad Rudolph

So how do I initilalise an array?

那么如何初始化一个数组呢?

Using the normal initialiser list syntax:

使用正常的初始化列表语法:

sig::sig() : p_list{1, 2, 3, 4}
{ }

Note, this only works in C++11. Before that, you need to use a boost::arrayan initialise it inside a function.

请注意,这仅适用于 C++11。在此之前,您需要boost::array在函数内使用初始化它。

回答by Some programmer dude

If your compiler doesn't support C++11 initialization, then you have to assign each field separatly:

如果您的编译器不支持 C++11 初始化,那么您必须单独分配每个字段:

p_list[0] = A;
p_list[1] = B;
p_list[2] = C;
p_list[3] = D;

回答by PaperBirdMaster

If your current compiler doesn't yet support C++11, you can initialize the vector contents using standard algorithms and functors:

如果您当前的编译器还不支持 C++11,您可以使用标准算法和函子初始化向量内容:

class sig
{
public:
    sig()
    {
        struct Functor
        {
            Functor() : value(0) {};
            int operator ()() { return value++; };
            int value;
        };
        std::generate(p_list, p_list + 4, Functor());
    }

    int p_list[4];
};

Previous snippet example here.

上一个片段示例在这里

Yes, is kind of ugly (at least, it looks ugly for me) and doesn't do the work at compile time; but it does the work that you need in the constructor.

是的,有点丑(至少,对我来说它看起来很丑)并且在编译时不做这项工作;但它在构造函数中完成了您需要的工作。

If you need some other kind of initialization (initialization with even/odd numbers, initialization with random values, start with anoter value, etc...) you only need to change the Functor, and this is the only advantage of this ugly approach.

如果您需要某种其他类型的初始化(偶数/奇数初始化、随机值初始化、从另一个值开始等),您只需要更改 Functor,这是这种丑陋方法的唯一优势。

回答by Mayur Lokare

You can initialise the array members like this using c++11 compiler using -std=c++11 or -std=gnu++11 option

您可以使用 c++11 编译器使用 -std=c++11 或 -std=gnu++11 选项初始化数组成员

struct student {
        private :
                int marks[5];
        public :
                char name[30];
                int rollno;
                student(int arr[], const char *name, int rno):marks{arr[0], arr[1], arr[2], arr[3], arr[4]}{
                        strcpy(this->name, name);
                        this->rollno = rno;
                }
                void printInfo() {
                        cout <<"Name : "<<this->name<<endl;
                        cout <<"Roll No : "<<this->rollno<<endl;
                        for(int i=0; i< 5; i++ ) {
                                cout <<"marks : "<<marks[i]<<endl;
                        }
                }
};
int main(int argc, char *argv[]) {
        int arr[] = {40,50,55,60,46};
        //this dynamic array passing is possible in c++11 so use option -std=c++11
        struct student s1(new int[5]{40, 50, 55, 60, 46}, "Mayur", 56);
        //can't access the private variable
        //cout <<"Mark1 : "<<s1.marks[0]<<endl;
        s1.printInfo();`enter code here`
} 

回答by Shahzeb

p_list[4] = {'A', 'B', 'C', 'D'};