是否可以在C ++的成员初始化器列表中初始化数组? [英] Is it possible to initialize an array in a member initializer list in C++?
本文介绍了是否可以在C ++的成员初始化器列表中初始化数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我有这个 Game
类,并且有一个 SDL_Rect
s数组.如果可能的话,我想在成员初始值设定项列表中对其进行初始化,而不是在构造函数体内初始化数组.
So, I have this Game
class, and I have an array of SDL_Rect
s. 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};
}
我想做这样的事情:
// Game.cpp
Game::Game()
: down[0]({1,4,31,48};
// etc, etc...
{}
推荐答案
您可以使用成员变量的直接列表初始化(自c ++ 11开始).(不是数组的每个元素.)
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}}
{}
这篇关于是否可以在C ++的成员初始化器列表中初始化数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文