如何声明没有特定大小的数组? [英] How to declare an array without specific size?
问题描述
如何将没有特定大小的数组声明为类成员?我想立即在类构造函数中设置此数组的大小.是否可以在不使用堆或不调整数组大小的情况下执行此操作?
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 thing without using the heap or without resizing the array?
推荐答案
C ++标准不允许使用可变长度数组.您有以下选项:
Variable length arrays are not allowed by C++ standard. The options you have are:
- 使用
std::vector
或 - 使用指向动态内存的指针
请注意,大多数编译器都将可变长度数组作为扩展来支持,因此,如果您不担心可移植性并且您的编译器支持它,则可以使用它.当然,它也有自己的问题,但是在您提到的限制条件下,它可以选择.
Note that Variable length arrays are supported by most compilers as a extension, So if you are not worried of portability and your compiler supports it, You can use it. ofcourse it has its own share of problems but its a option given the constraints you cited.
这篇关于如何声明没有特定大小的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!