【发布时间】:2020-02-03 00:49:59
【问题描述】:
使用聚合类时,传入数组成员的初始化列表不是问题。例如:
class color_def
{
public:
const u8 color_passes;
const u8 red_shift[MAX_COLOR_PASSES];
const u8 red_bitcount[MAX_COLOR_PASSES];
const u8 green_shift[MAX_COLOR_PASSES];
const u8 green_bitcount[MAX_COLOR_PASSES];
const u8 blue_shift[MAX_COLOR_PASSES];
const u8 blue_bitcount[MAX_COLOR_PASSES];
};
const static color_def snk_neogeo_color = {
3, {15, 14, 8}, {1, 1, 4}, {15, 13, 4}, {1, 1, 4}, {15, 12, 0}, {1, 1, 4}};
这没有任何问题。但是,当我添加显式构造函数时,它将不起作用。例如,来自类似的类:
class chr_def
{
public:
chr_def(u16 width, u16 height, u8 bitplanes, u32 planeoffset[MAX_GFX_PLANES],
u32 xoffset[MAX_GFX_SIZE], u32 yoffset[MAX_GFX_SIZE],
const u8 *(*converter)(const chr_def *chrdef, const u8 *data))
: width(width),
height(height),
bitplanes(bitplanes),
planeoffset{planeoffset},
xoffset(xoffset),
yoffset(yoffset),
converter(converter),
datasize(width * height * bitplanes){};
const u8 *convert(const u8 *data) { return this->converter(this, data); }
const u16 width; // pixel width of each element
const u16 height; // pixel height of each element
const u8 bitplanes; // number of color bitplanes
const u32 *planeoffset; // bit offset of each bitplane
const u32 *xoffset; // bit offset of each horizontal pixel
const u32 *yoffset; // bit offset of each vertical pixel
const u32 datasize; // size of one chr in bits
const u8 *(*converter)(const chr_def *, const u8 *);
};
const static chr_def nintendo_sfc = {
8,
8,
4,
{0, 8, 128, 136},
{0, 1, 2, 3, 4, 5, 6, 7},
{0 * 16, 1 * 16, 2 * 16, 3 * 16, 4 * 16, 5 * 16, 6 * 16, 7 * 16},
get_chr};
失败,抱怨参数与构造函数不匹配。问题似乎是它将数组初始化列表的第一个值作为指向数组的指针:
note: candidate constructor not viable: cannot convert initializer list argument to 'u32 *' (aka 'unsigned int *')
如果我将数组初始化列表拉出并预先将它们初始化为自己的变量,如下所示:
u32 temp1[4] = {0, 8, 128, 136};
u32 temp2[8] = {0, 1, 2, 3, 4, 5, 6, 7};
u32 temp3[8] = {0 * 16, 1 * 16, 2 * 16, 3 * 16, 4 * 16, 5 * 16, 6 * 16, 7 * 16};
const static chr_def nintendo_sfc = {
8,
8,
4,
temp1,
temp2,
temp3,
get_chr};
一切正常。如果可能的话,我真的更希望不这样做。
那么...为什么这在聚合形式中有效,但不适用于我的显式构造函数?有什么办法可以使这项工作?感谢任何人可以提供的任何见解。
【问题讨论】:
-
数组大小是否依赖于
width/height/bitplanes?如果是这样,您应该考虑将它们设为模板参数。 -
指针是单个值。用多个值初始化它是没有意义的。使用指针初始化指针(在有效的示例中,数组衰减为指针)。您是否考虑过使用
std::array代替哑数组?
标签: c++ arrays constructor initializer-list