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

发布于 2024-07-14 04:03:08 字数 265 浏览 7 评论 0原文

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

我的想法可能看起来像这样,(当然)这是行不通的:

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

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

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

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

此刻的回忆 2024-07-21 04:03:08

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

您将像这样声明二维(假设整数数组):

int ** _array;

然后在您的测试方法中,您需要首先分配指针数组,然后为每个指针分配一个整数数组:

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

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

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

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.

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:

_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;
贪了杯 2024-07-21 04:03:08

矢量是你最好的朋友

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

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

vector is your best friend

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

    public:
    Test(int width, int height) :
        _array(width,vector<int>(height,0))
    {
    }
};
七婞 2024-07-21 04:03:08

我认为您是时候查找新的/删除的运算符了。

由于这是一个多维数组,因此您必须在调用“new”时循环调用(再次不要忘记:删除)。

尽管我确信很多人会建议使用具有宽度*高度元素的一维数组。

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).

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

青芜 2024-07-21 04:03:08

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

// 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];

}

(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];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文