Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否可以在C+;中的成员初始值设定项列表中初始化数组+;?_C++_Arrays_Initialization_Member_Initializer List - Fatal编程技术网

C++ 是否可以在C+;中的成员初始值设定项列表中初始化数组+;?

C++ 是否可以在C+;中的成员初始值设定项列表中初始化数组+;?,c++,arrays,initialization,member,initializer-list,C++,Arrays,Initialization,Member,Initializer List,因此,我有一个Game类,我有一个SDL\u Rects数组。如果可能的话,我想在成员初始值设定项列表中初始化它,而不是在构造函数体中初始化数组 //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_W

因此,我有一个
Game
类,我有一个
SDL\u Rect
s数组。如果可能的话,我想在成员初始值设定项列表中初始化它,而不是在构造函数体中初始化数组

//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};
}
我想这样做:

// Game.cpp
Game::Game()
: down[0]({1,4,31,48};
  // etc, etc...
{}
std::array<Rect, 4> down =
{{
    {1,4,31,48},
    {35,5,25,47},
    {65,4,31,48},
    {100,5,26,47}
}};
#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;
}
没问题

所以这是一个令人困惑的问题

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;
}

将初始化放置在构造函数的成员初始值设定项列表中(如果出于某种需要,而不是上述原因),可以如下所示:

// Game.cpp
Game::Game()
: down[0]({1,4,31,48};
  // etc, etc...
{}
std::array<Rect, 4> down =
{{
    {1,4,31,48},
    {35,5,25,47},
    {65,4,31,48},
    {100,5,26,47}
}};
#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;
}
#包括
struct Rect{int x,y,width,height;};
结构游戏
{
std::数组向下;
游戏()
:向下{{
{1,4,31,48},
{35,5,25,47},
{65,4,31,48},
{100,5,26,47}
}}
{}
};
#包括
使用名称空间std;
auto main()->int
{
游戏g;
for(矩形常数和矩形:g.down)
{
cout可以(从c++11开始)对成员变量使用(不是数组的每个元素)


我同意,我正试图让构建环境设置来测试它,因为我认为我肯定遗漏了什么。很抱歉,如果未完成的评论在我成功删除之前停留太久。就目前而言,这是一个确定的答案。