Determine array size in constructor initializer

前端 未结 12 2398
失恋的感觉
失恋的感觉 2020-12-08 21:10

In the code below I would like array to be defined as an array of size x when the Class constructor is called. How can I do that?

class Class
{
public:
  int         


        
12条回答
  •  旧巷少年郎
    2020-12-08 21:49

    You can't initialize the size of an array with a non-const dimension that can't be calculated at compile time (at least not in current C++ standard, AFAIK).

    I recommend using std::vector instead of array. It provides array like syntax for most of the operations.

提交回复
热议问题