Determine array size in constructor initializer

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

    Instead of using a raw array, why not use a vector instead.

    class SomeType {
      vector v;
      SomeType(size_t x): v(x) {}
    };
    

    Using a vector will give you automatic leak protection in the face of an exception and many other benefits over a raw array.

提交回复
热议问题