将初始化列表传递给 C++ 中的数组成员
Passing an initialization list to an array member in C++
使用聚合类时,传入数组成员的初始值设定项列表不是问题。例如:
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 *')
如果我拉出数组 init 列表并事先将它们初始化为它们自己的变量,如下所示:
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};
一切正常。如果可能的话,我真的不想这样做。
那么......为什么这在聚合形式中有效,而不适用于我的显式构造函数?有没有办法做到这一点?感谢任何人都可以提供的任何见解。
您遇到的问题是您正在尝试使用std::initializer_list
初始化指针成员,这根本不可能。
但是,您可以使用std::array
(从 c++ 11 开始(。下面是一个简化的示例:
#include <iostream>
#include <array>
#define MAX_GFX_PLANES (4U)
struct chr_def {
// Constructor
chr_def(size_t size, const std::array<unsigned int, MAX_GFX_PLANES>& planeoffset) :
size(size), planeoffset(planeoffset) { };
// Data Members
const size_t size;
const std::array<unsigned int, MAX_GFX_PLANES> planeoffset;
};
const static chr_def nintendo_sfc{ 8U, {0U, 1U, 2U, 3U} };
int main() {
// std::array can be used with range-based for
for (auto i : nintendo_sfc.planeoffset) {
std::cout << i << " ";
}
return 0;
}
给你带来麻烦的不是显式构造函数,而是你正在使用的指针代替数组:
以下变量声明:
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 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
指针与数组不同。
您可以了解它们之间的区别:数组名称是指针吗?
若要启用这些指针,必须在构造函数中接收接受initializer_list的类型,然后在函数中必须为指针分配内存中的新位置,然后将数组中的值复制到新分配的指针。
相关文章:
- 为什么我无法访问指向数组中成员函数的指针?
- 如何将结构/联合数组的成员传递到函数中
- 使用带有参数包的数组的成员数组初始化类
- 有没有办法给数组的成员指定不同的名称?
- 使用包含结构的数组的成员函数
- C++:释放动态数组(结构成员)和指向此结构的指针的方法
- C 用数组作为成员创建对象
- 用另一个 constexpr 数组对成员数组进行大括号初始化
- 来自数组的成员和比特菲尔德的结构
- 如何创建指向数组类成员的指针?
- 将数组的成员设置为零
- "Empty"数组\向量成员 C++
- 功能指针数组(包括成员功能)投掷模板专业化错误
- 另一个结构内部的结构数组的成员变量没有通过引用传递
- 正确转换 2D 数组的成员变量
- 常量数组类成员初始化
- 为具有对象或对象数组作为成员的类运行不同的代码
- C++ 如何指向数组的成员
- 将包含数组数据成员的结构保存到文件
- 数组类成员初始化(C++)