Trts
Trts

Reputation: 1069

How to use initializer_list

I have:

1) NetBeans 7.3 2) Qt by Digia v4.8.4. 3) MinGW compiler.

Could you help me initialize my array? I have failed, unfortunately. I understand that I have to use initializer_list. But what to include into my files and how to organize everything is a mystery to me.

Will you be so kind as to help me?

Figure.h

#include <initializer_list>

class Figure: public QObject{
    Q_OBJECT
        private:        
                int shape[4][4][4];

Figure.cpp

Figure:: Figure(){

std::initializer_list<int> init;
auto init = std::initializer_list<int>
(    
            {                        
                            {0, 0, 0, 0}, 
                            {0, 1, 1, 0},
                            {0, 0, 1, 0},
                    {0, 0, 0, 0}
                    ,                        
                            {0, 0, 0, 0}, 
                            {0, 0, 1, 0},
                            {0, 1, 1, 0},
                    {0, 0, 0, 0}
                    ,                        
                            {0, 0, 0, 0}, 
                            {0, 1, 0, 0},
                            {0, 1, 1, 0},
                    {0, 0, 0, 0}
                    ,                        
                            {0, 0, 0, 0}, 
                            {0, 1, 1, 0},
                            {0, 1, 0, 0},
                    {0, 0, 0, 0}                        
            }
    );  

Upvotes: 5

Views: 1003

Answers (2)

Alexander Shukaev
Alexander Shukaev

Reputation: 17021

...
  private:        
    int shape[4][4][4] {                        
                           {{0, 0, 0, 0}, 
                            {0, 1, 1, 0},
                            {0, 0, 1, 0},
                            {0, 0, 0, 0}}
                    ,                        
                           {{0, 0, 0, 0}, 
                            {0, 0, 1, 0},
                            {0, 1, 1, 0},
                            {0, 0, 0, 0}}
                    ,                        
                           {{0, 0, 0, 0}, 
                            {0, 1, 0, 0},
                            {0, 1, 1, 0},
                            {0, 0, 0, 0}}
                    ,                        
                           {{0, 0, 0, 0}, 
                            {0, 1, 1, 0},
                            {0, 1, 0, 0},
                            {0, 0, 0, 0}}                        
    };
...

In your particular case, you don't even need to use initalizer_list explicitly because static array can be initialized in the place of definition, and this is done via the syntax in the example above (requires C++11 compliance).

Initialization of shape in constructor initialization list is possible too:

...
Figure::Figure(): shape {
                               {{0, 0, 0, 0}, 
                                {0, 1, 1, 0},
                                {0, 0, 1, 0},
                                {0, 0, 0, 0}}
                        ,                        
                               {{0, 0, 0, 0}, 
                                {0, 0, 1, 0},
                                {0, 1, 1, 0},
                                {0, 0, 0, 0}}
                        ,                        
                               {{0, 0, 0, 0}, 
                                {0, 1, 0, 0},
                                {0, 1, 1, 0},
                                {0, 0, 0, 0}}
                        ,                        
                               {{0, 0, 0, 0}, 
                                {0, 1, 1, 0},
                                {0, 1, 0, 0},
                                {0, 0, 0, 0}}
  } {
  ...
}
...

NOTE: Pay attention to the fact that you've missed additional parentheses in your try.

Upvotes: 1

Andy Prowl
Andy Prowl

Reputation: 126432

You do not need to create an initializer_list, especially because you would then be trying to assign it it to your array, rather than initialize your array from it - and that would be illegal. Rather, you could initialize your array directly in the constructor's initialization list, as done below:

Figure::Figure()
    : shape
    {
        {
            {0, 0, 0, 0},
            {0, 1, 1, 0},
            {0, 0, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 0, 1, 0},
            {0, 1, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 1, 0, 0},
            {0, 1, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 1, 1, 0},
            {0, 1, 0, 0},
            {0, 0, 0, 0}
        }
    }
{
    // Body of the constructor...
};

Notice that in C++11 you could also provide an inline initializer for your array, as done below:

class Figure: public QObject {
    Figure();
private:
    int shape[4][4][4] {
        {
            {0, 0, 0, 0},
            {0, 1, 1, 0},
            {0, 0, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 0, 1, 0},
            {0, 1, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 1, 0, 0},
            {0, 1, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 1, 1, 0},
            {0, 1, 0, 0},
            {0, 0, 0, 0}
        }
    };
};

Figure::Figure() {
    // Body of the constructor...
}

Upvotes: 0

Related Questions