Determine array size in constructor initializer

前端 未结 12 2415
失恋的感觉
失恋的感觉 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:48

    Use the new operator:

    class Class
    {
       int* array;
       Class(int x) : array(new int[x]) {};
    };
    

提交回复
热议问题