Is there a way to statically-initialize a dynamically-allocated array in C++?

前端 未结 6 848
囚心锁ツ
囚心锁ツ 2020-12-03 17:40

In C++, I can statically initialize an array, e.g.:

int a[] = { 1, 2, 3 };

Is there an easy way to initialize a dynamically-allocated array

6条回答
  •  执笔经年
    2020-12-03 18:03

    No, you cannot initialize a dynamically created array in the same way.

    Most of the time you'll find yourself using dynamic allocation in situations where static initialization doesn't really make sense anyway. Such as when you have arrays containing thousands of items. So this isn't usually a big deal.

提交回复
热议问题