Is there a way to initialize an array with non-constant variables? (C++)

后端 未结 9 2159
無奈伤痛
無奈伤痛 2020-12-06 11:20

I am trying to create a class as such:

class CLASS
{
public:
    //stuff
private:
    int x, y;
    char array[x][y];
};

Of course, it does

9条回答
  •  生来不讨喜
    2020-12-06 11:57

    If you want a dynamically sized array as a class member, you need to array new it and assign that value to a pointer. The char array[size] syntax is only for statically-sized arrays.

    Better yet, you really should use an std::vector< std::vector >, there are very few good reasons to manually work with dynamically sized arrays these days.

提交回复
热议问题