在C ++中延迟类定义中的数组大小?

时间:2022-06-04 16:59:55

Is there some way to delay defining the size of an array until a class method or constructor?

有没有办法延迟定义数组的大小,直到类方法或构造函数?

What I'm thinking of might look something like this, which (of course) doesn't work:

我在想什么可能看起来像这样,(当然)不起作用:

class Test
{
    private:
    int _array[][];

    public:
    Test::Test(int width, int height);
};

Test::Test(int width, int height)
{
    _array[width][height];
}

4 个解决方案

#1


8  

What Daniel is talking about is that you will need to allocate memory for your array dynamically when your Test (width, height) method is called.

Daniel所说的是,当调用Test(width,height)方法时,您需要动态地为数组分配内存。

You would declare your two dimensional like this (assuming array of integers):

你会像这样声明你的二维(假设整数数组):

int ** _array;

And then in your Test method you would need to first allocate the array of pointers, and then for each pointer allocate an array of integers:

然后在你的Test方法中你需要先分配指针数组,然后为每个指针分配一个整数数组:

_array = new  *int [height];
for (int i = 0; i < height; i++)
{
    _array [i] = new int[width];
}

And then when the object is released you will need to explicit delete the memory you allocated.

然后当释放对象时,您需要显式删除分配的内存。

for (int i = 0; i < height; i++)
{
    delete [] _array[i];
    _array [i] = NULL;
}
delete [] _array;
_array = NULL;

#2


8  

vector is your best friend

矢量是你最好的朋友

class Test
{
    private:
    vector<vector<int> > _array;

    public:
    Test(int width, int height) :
        _array(width,vector<int>(height,0))
    {
    }
};

#3


2  

I think it is time for you to look up the new/delete operators.

我认为是时候查找新的/删除操作符了。

Seeing as this is a multidimensional array, you're going to have to loop through calling 'new' as you go (and again not to forget: delete).

看到这是一个多维数组,你将不得不循环调用'new'(同样不要忘记:删除)。

Although I am sure many will suggest to use a one-dimensional array with width*height elements.

虽然我相信很多人会建议使用带有* width元素的一维数组。

#4


1  

(Months later) one can use templates, like this:

(几个月后)可以使用模板,如下所示:

// array2.c
// http://www.boost.org/doc/libs/1_39_0/libs/multi_array/doc/user.html
// is professional, this just shows the principle

#include <assert.h>

template<int M, int N>
class Array2 {
public:
    int a[M][N];  // vla, var-len array, on the stack -- works in gcc, C99, but not all

    int* operator[] ( int j )
    {
        assert( 0 <= j && j < M );
        return a[j];
    }

};

int main( int argc, char* argv[] )
{
    Array2<10, 20> a;
    for( int j = 0; j < 10; j ++ )
    for( int k = 0; k < 20; k ++ )
        a[j][k] = 0;

    int* failassert = a[10];

}

#1


8  

What Daniel is talking about is that you will need to allocate memory for your array dynamically when your Test (width, height) method is called.

Daniel所说的是,当调用Test(width,height)方法时,您需要动态地为数组分配内存。

You would declare your two dimensional like this (assuming array of integers):

你会像这样声明你的二维(假设整数数组):

int ** _array;

And then in your Test method you would need to first allocate the array of pointers, and then for each pointer allocate an array of integers:

然后在你的Test方法中你需要先分配指针数组,然后为每个指针分配一个整数数组:

_array = new  *int [height];
for (int i = 0; i < height; i++)
{
    _array [i] = new int[width];
}

And then when the object is released you will need to explicit delete the memory you allocated.

然后当释放对象时,您需要显式删除分配的内存。

for (int i = 0; i < height; i++)
{
    delete [] _array[i];
    _array [i] = NULL;
}
delete [] _array;
_array = NULL;

#2


8  

vector is your best friend

矢量是你最好的朋友

class Test
{
    private:
    vector<vector<int> > _array;

    public:
    Test(int width, int height) :
        _array(width,vector<int>(height,0))
    {
    }
};

#3


2  

I think it is time for you to look up the new/delete operators.

我认为是时候查找新的/删除操作符了。

Seeing as this is a multidimensional array, you're going to have to loop through calling 'new' as you go (and again not to forget: delete).

看到这是一个多维数组,你将不得不循环调用'new'(同样不要忘记:删除)。

Although I am sure many will suggest to use a one-dimensional array with width*height elements.

虽然我相信很多人会建议使用带有* width元素的一维数组。

#4


1  

(Months later) one can use templates, like this:

(几个月后)可以使用模板,如下所示:

// array2.c
// http://www.boost.org/doc/libs/1_39_0/libs/multi_array/doc/user.html
// is professional, this just shows the principle

#include <assert.h>

template<int M, int N>
class Array2 {
public:
    int a[M][N];  // vla, var-len array, on the stack -- works in gcc, C99, but not all

    int* operator[] ( int j )
    {
        assert( 0 <= j && j < M );
        return a[j];
    }

};

int main( int argc, char* argv[] )
{
    Array2<10, 20> a;
    for( int j = 0; j < 10; j ++ )
    for( int k = 0; k < 20; k ++ )
        a[j][k] = 0;

    int* failassert = a[10];

}