How to declare an array without specific size?

后端 未结 4 1353
情书的邮戳
情书的邮戳 2021-01-02 20:06

How can I declare an array without specific size as a class member? I want to set the size of this array immediately in the class constructor. Is it possible to do such thin

4条回答
  •  忘掉有多难
    2021-01-02 20:46

    Class member array have to be declared with exact compile-time size. There's no way around it.

    The only way you can declare an array as an immediate member of the class and yet be able to decide its size at run time would be the popular "struct hack" technique inherited from C.

    Other than that, you have to declare your array as an indirect member of the class: either declare a member of pointer type and allocate memory later, or use some library implementation of run-time sized array (like std::vector)

提交回复
热议问题