C ++动态/静态数组作为函数的参数 [英] C++ Dynamic/Static arrays as parameters to functions

查看:143
本文介绍了C ++动态/静态数组作为函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用二维数组作为参数的构造在我的C ++类。

I have been trying to use 2D array as a parameter to a constructor in my C++ class.

标题:

Matrix::Matrix(double **initComponents, int rows, int columns)

如果我先走一步,做定期调用新的初始化子数组,指针传递给一个构造函数,其所有的好,但我在想,我可以创建静态二维数组初始化和指针传递给它,一个构造?

If I just go ahead and do regular calls to new and initialize sub arrays, pass a pointer to a constructor, its all good BUT I was thinking , can I just create static 2D array with initializer and pass a pointer to it, to a constructor?

double A[][3] = { {2.0, 3.0, 4.0} , {1.0, 3.0, 4.0} , {2.0, 3.0, 4.0} }; 
Matrix *mtx = new Matrix(A, 3, 3);

这不工作:(

MS Visual Studio的编译器:

MS Visual Studio compiler :

" error C2664: 'Matrix::Matrix(double **,int,int)' : cannot convert parameter 1 from 'double [3][3]' to 'double ** "

任何人能解释为什么吗?我以为它会做参考和指针之间的自动转换。

Can anyone explain why? I thought it will do an automatic conversion between reference and a pointer.

第二个问题是,为什么我可以只申报 A 为双 A [] [] ,为什么我必须指定第​​二个维度,即使我指定所有的值。

Second question is, why can I just declare A as double A[][], why do I have to specify the second dimension, even though I specify all values.

推荐答案

这是不数组转换为一个指针,但它仅适用于一个级别。数组的数组不能转换为指针的指针。

An array does convert to a pointer, but it only works for one level. An array of arrays does not convert to a pointer to pointers.

的阵列可以被转换为一个指针,因为指针仍提供了一种合理的方式来查找阵列的值,但数组的数组比指针的指针根本不同的事情。数组的数组存储在内存中连续。例如,如果你有一个定义是这样的:

An array can be converted to a pointer because a pointer still provides a reasonable way to look up the values of the array, but an array of arrays is a fundamentally different thing than a pointer to pointers. An array of arrays is stored in memory contiguously. For example, if you had a definition like this:

int a[2][2] = {{1,2},{3,4}};

然后,[0] [0]被存储在第一存储器位置中,[0] [1]存储在第二,一[1] [0]在第三和[1] [1在第四。当你想看起来像一个由[i] [j]的一个特定的元素,编译器可以计算位置,因为它知道数组的大小:

Then a[0][0] is stored in the first memory location, a[0][1] is stored in the second, a[1][0] in the third, and a[1][1] in the fourth. When you want to look up a particular element like a[i][j], the compiler can compute the location since it knows the size of the arrays:

int value = ((int *)a)[i*2+j];

比较这对一个指针的指针

Compare this to a pointer to pointers

int a0[2] = {1,2};
int a1[2] = {3,4};
int **a = {a0,a1};

现在,当您使用[I] [J],编译器不能直接知道去哪里找。它首先看一个[0],看看指向,然后看第二次发现的价值。

Now when you use a[i][j], the compiler can't directly know where to look. It first has to look at a[0] and see where that points to, and then look a second time to find the value.

int *ap = a[i];
int value = ap[j];

由于编译器使用的数组的数组和指针仰视值指针方式不一样,你不能互换使用它们。

Because the compiler has to use a different way of looking up the values in arrays of arrays and pointers to pointers, you can't use them interchangeably.

同样,在这样的声明:

int a[][2] = {{1,2},{3,4}};

它是好的,离开了第一维的大小,因为编译器会填写为你,但这仅适用于一个级别。原则上,编译器可以做出其他的尺寸应该是什么好猜测,但它真的会在语言一个单独的规则。

it is okay to leave off the size of the first dimension because the compiler will fill it in for you, but this only works for one level. In principle, the compiler could make a good guess about what the other dimensions should be, but it would really be a separate rule in the language.

如果你真的希望能够传递一个二维数组,你可以用这样的模板构造做到这一点:

If you really want to be able to pass a 2D array, you can do it with a templated constructor like this:

template<int rows, int cols>
Matrix(const double (&initComponents)[rows][cols])
{
  // initialize matrix elements here
}

然后,你甚至都不需要明确地传递尺寸:

And then you don't even need to pass the size explicitly:

double A[][3] = { {2.0, 3.0, 4.0} , {1.0, 3.0, 4.0} , {2.0, 3.0, 4.0} };
Matrix *mtx = new Matrix(A);

这篇关于C ++动态/静态数组作为函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆