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

后端 未结 9 2116
無奈伤痛
無奈伤痛 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 <vector>
    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<std::vector<char> > my_array;
    };
    
    0 讨论(0)
  • 2020-12-06 11:57

    You can allocate memory to your 2-dimensional array in the constructor and free it in the destructor. The simplest way:

    array = (char **)malloc(sizeof(char *) * x);
    if (array) {
        for (i = 0; i < x; i++) {
            array[i] = (char *)malloc(sizeof(char) * y);
            assert(array[i]);
        }
    }
    
    0 讨论(0)
  • 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<char> >, there are very few good reasons to manually work with dynamically sized arrays these days.

    0 讨论(0)
  • 2020-12-06 12:00

    You can't allocate or initialize a global or static array declaratively using non-constant values (compile-time). It's possible for local arrays though (C99 variable sized arrays, as their initializer essentially runs at runtime every time the function is executed).

    For your situation, I suggest using a pointer instead of an array and create the actual array dynamically at runtime (using new):

    class CLASS
    {
    public:
        CLASS(int _x, int _y) : x(_x), y(_y) {
           array = new char*[x];
           for(int i = 0; i < x; ++i)
               array[i] = new char[y];
        }
        ~CLASS() {
           for (int i = 0; i < x; ++i)
               delete[] array[i];
           delete[] array;
        }
        //stuff
    private:
        int x, y;
        char **array;
    };
    
    0 讨论(0)
  • 2020-12-06 12:02

    If the size is not known at compile time, the array is dynamic. What you could do to keep it static is to make them larger than your largest expected size.

    0 讨论(0)
  • 2020-12-06 12:07

    The compiler need to have the exact size of the class when compiling, you will have to use the new operator to dynamically allocate memory.

    Switch char array[x][y]; to char** array; and initialize your array in the constructor, and don't forget to delete your array in the destructor.

    class MyClass
    {
    public:
        MyClass() {
            x = 10; //read from file
            y = 10; //read from file
            allocate(x, y);
        }
    
        MyClass( const MyClass& otherClass ) {
            x = otherClass.x;
            y = otherClass.y;
            allocate(x, y);
    
            // This can be replace by a memcopy
            for( int i=0 ; i<x ; ++i )
                for( int j=0 ; j<x ; ++j )
                    array[i][j] = otherClass.array[i][j];
        }
    
        ~MyClass(){
            deleteMe();
        }
    
        void allocate( int x, int y){
            array = new char*[x];
            for( int i = 0; i < y; i++ )
                array[i] = new char[y];
        }
    
        void deleteMe(){
            for (int i = 0; i < y; i++)
               delete[] array[i];
            delete[] array;
        }
    
        MyClass& operator= (const MyClass& otherClass)
        {
            if( this != &otherClass )
            {
                deleteMe();
                x = otherClass.x;
                y = otherClass.y;
                allocate(x, y);
                for( int i=0 ; i<x ; ++i )
                    for( int j=0 ; j<y ; ++j )
                        array[i][j] = otherClass.array[i][j];            
            }
            return *this;
        }
    private:
        int x, y;
        char** array;
    };
    

    *EDIT: I've had the copy constructor and the assignment operator

    0 讨论(0)
提交回复
热议问题