Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to initialize an array in a member initializer list in C++?

So, I have this Game class, and I have an array of SDL_Rects. I would like to initialize it in the member initializer list if that's possible, instead of initializing the array inside the constructor body.

//Game.h
#pragma once

class Game {
public:
  Game(SDL_Window* window, SDL_Renderer* renderer);

private:
  SDL_Rect down[4];
};

// Game.cpp
#include "Game.h"

Game::Game(SDL_Window* window, SDL_Renderer* renderer){
  down[0] = {1,4,31,48};
  down[1] = {35,5,25,47};
  down[2] = {65,4,31,48};
  down[3] = {100,5,26,47};
}

I would like to do something like this:

// Game.cpp
Game::Game()
: down[0]({1,4,31,48};
  // etc, etc...
{}
like image 920
Vince Avatar asked Nov 30 '15 04:11

Vince


People also ask

How array can be initialized by initializer method?

There are two ways to specify initializers for arrays: With C89-style initializers, array elements must be initialized in subscript order. Using designated initializers, which allow you to specify the values of the subscript elements to be initialized, array elements can be initialized in any order.

Can you initialize an empty array in C?

Technically you can't make an array empty. An array will have a fixed size that you can not change. If you want to reset the values in the array, either copy from another array with default values, or loop over the array and reset each value.

Are arrays in C automatically initialized?

The elements of global and static arrays, on the other hand, are automatically initialized with their default values, which for all fundamental types this means they are filled with zeros.


2 Answers

You could use direct-list-initialization (since c++11) for the member variable. (Not every element of the array.)

Game::Game()
: down {{1,4,31,48}, {35,5,25,47}, {65,4,31,48}, {100,5,26,47}}
{}

LIVE

like image 106
songyuanyao Avatar answered Oct 05 '22 22:10

songyuanyao


There is no problem.

So it's a baffling question.

struct Rect { int x, y, width, height; };

struct Game
{
    Rect down[4] =
    {
        {1,4,31,48},
        {35,5,25,47},
        {65,4,31,48},
        {100,5,26,47},
    };
};

#include <iostream>
using namespace std;
auto main() -> int
{
    Game g;
    for( Rect const& rect : g.down )
    {
        cout << rect.x << ' ';
    }
    cout << endl;
}

In order to use a std::array instead of the raw array, which is generally a Good Idea™, and have the code compile with g++, add an inner set of braces to the initializer, like this:

std::array<Rect, 4> down =
{{
    {1,4,31,48},
    {35,5,25,47},
    {65,4,31,48},
    {100,5,26,47}
}};

Placing the initialization in a constructor's member initializer list (if for some reason that's desired, instead of the above) can then look like this:

#include <array>

struct Rect { int x, y, width, height; };

struct Game
{
    std::array<Rect, 4> down;

    Game()
        : down{{
            {1,4,31,48},
            {35,5,25,47},
            {65,4,31,48},
            {100,5,26,47}
        }}
    {}
};

#include <iostream>
using namespace std;
auto main() -> int
{
    Game g;
    for( Rect const& rect : g.down )
    {
        cout << rect.x << ' ';
    }
    cout << endl;
}
like image 43
Cheers and hth. - Alf Avatar answered Oct 05 '22 22:10

Cheers and hth. - Alf