【发布时间】:2017-01-02 14:07:30
【问题描述】:
在 c++17 中是否有一种方法可以使用默认构造函数以外的其他构造函数在堆栈中构造数组。
这是一种特殊情况,当每个数组值都使用相同的构造函数参数构造时。
我需要使用位于堆栈的基本数组(不是向量或指针数组或其他东西)。
这段代码说明了我想要做什么:
#include <iostream>
using namespace std;
struct A {
A() {
printf("A() called\n");
}
A(int i, int j) {
printf("A(%d, %d) called\n", i, j);
}
};
struct B {
A tab[100];
B(int i, int j) {
// I would like to call A(i, j) for each tab entries instead of the default constructor
printf("B(%d, %d) called\n", i, j);
}
};
int main() {
B b(1, 7);
return 0;
}
谢谢
【问题讨论】: