Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In C++11 only:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...