【发布时间】:2021-01-02 13:06:52
【问题描述】:
我对 cpp 完全陌生,但知道一些 python。我想创建一个类,它有一个数组作为属性,其大小在构造函数中给出。这是我想做的,但在 python 中:
class test:
def __init__(self,size):
self.arr = [x for x in range(size)]
这就是我在 c++ 中所拥有的:
class Field{
public:
int width;
int height;
int field[];
Field(int _width, int _height){
width = _width;
height = _height;
field = new int[width*height];
}
};
但是在声明字段的时候我需要提供一个大小,但是这个大小是后面才给出的。我该怎么做?
【问题讨论】: