Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use initializer_list

Tags:

c++

c++11

qt

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}                        
            }
    );  
like image 810
Trts Avatar asked Nov 12 '22 06:11

Trts


1 Answers

...
  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.

like image 199
Alexander Shukaev Avatar answered Nov 15 '22 06:11

Alexander Shukaev