是否可以在执行new的时候在构造函数中传递不同的参数?

Is it possible to pass different arguments in the constructor during the execution of new?

假设我有一个 class 及其构造函数:

Class MyClass {
  public:
     MyClass(int arg);
  private:
     int a;
};

和一个全局数组:

int MyArray[]={1,2,3,4,5,6,7,8,9,10}

我想要一个指向 MyClass 的动态指针数组,但数组的每个元素都必须使用不同的编号调用构造函数。我正在尝试做类似的事情,但没有成功

int main()
{
   int i=0;
   MyClass *MyDynArray = new MyClass[10]{MyArray[i++]};
}

是否可以在不使用向量的情况下在 C++ 中执行此操作?提前致谢!!

您可以像声明具有自动或静态存储持续时间的数组一样进行操作。

MyClass *MyDynArray = new MyClass[10]{1,2,3,4,5,6,7,8,9,10};