我们可以用参数化构造函数初始化结构的数组吗?
Can we initialize an array for a structure with a parametrized constructor?
我们可以为具有默认构造函数的结构初始化数组,但是我们可以使用参数化构造函数来做到这一点吗?
struct class{
int room;
int floor;
class(){
room=0;
floor=0;
}
};
int main(){
class c1[5];
}
上面的代码工作正常。但是,当有一个参数化的构造函数时该怎么办?
struct class{
int room;
int floor;
class(int r,int f){
room=r;
floor=f;
}
};
您可以使用列表初始化:
struct test {
test(int,int);
};
test c1[] = {
{1, 2},
{3, 4},
{5, 6},
};
相关文章:
- C++将文本文件中的数据读取到结构数组中
- JSON转换为nlohmann JSON-lib中的结构数组
- 需要帮助将结构数组传递给函数
- 在 64 位边界上对齐C++结构数组?
- 带有枚举方向/类型的气泡排序结构数组
- 在C++中访问结构数组中的数据
- 从指向结构数组的指针中提取元素
- 如何制作一个地图,其中的值是C++中的结构数组
- 如何在函数中显示结构数组
- 包含作为成员C++函数的结构数组
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- std::transform 将一个结构数组复制到另一个结构数组
- 将非托管结构数组封送到托管数组
- 显示结构数组
- 结构数组中所有类似字段的总和
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 在 glsl opengl 中将嵌套结构数组作为统一传递
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 如何将字符串字符与结构数组进行比较?
- C++结构数组