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

后端 未结 9 2161
無奈伤痛
無奈伤痛 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:55

    use vector.

    #include 
    class YourClass
    {
    public:
        YourClass()
        : x(read_x_from_file()), y(read_y_from_file())
        {
            my_array.resize(x);
            for(int ix = 0; ix < x; ++ix)
                my_array[ix].resize(y);
        }
    
        //stuff
    
    private:
        int x, y;
        std::vector > my_array;
    };
    

提交回复
热议问题