例えば以下のようなプログラムがあったとき、21行目にてクラスの動的確保を行っているのですが、コントラスタに引数が必要な場合、
以下のような書き方などではコンパイルエラーになってしまいます。
どのように書けばエラーにならないのかお教えください(初期化は何行になっても構いません)
#include <iostream>
#include <random>
using namespace std;
class A {
private:
int a;
public:
A(int num) : a(num) { }
void show_a() { cout << a << endl; }
};
int main()
{
int test_num = 5;
A *test;
random_device rand_num;
mt19937 mt(rand_num());
//こ↑こ↓---------------------------
test = new A[test_num](mt() % 5 + 1);
//-----------------------------------
for (int i = 0; i < test_num; i++) {
test[i].show_a();
}
delete[] test;
return 0;
}