【发布时间】:2012-07-22 14:20:36
【问题描述】:
在我关于passing array as const argument 的问题之后,我试图弄清楚如何编写一个方法,其中参数是固定大小的 const 数组的 const 数组。唯一可写的就是这些数组的内容。
我正在考虑这样的事情:
template <size_t N>
void myMethod(int* const (&inTab)[N])
{
inTab = 0; // this won't compile
inTab[0] = 0; // this won't compile
inTab[0][0] = 0; // this will compile
}
这个解决方案的唯一问题是我们不知道第一个维度。 有人对此有解决方案吗?
提前致谢,
凯文
[编辑]
我不想使用 std::vector 或这种动态分配的数组。
【问题讨论】:
-
The only writable thing would be the content of these arrays.和the argument is a const array不能很好地结合在一起 -
我不确定我是否理解,但是传递一个数组数组是这样完成的:
template <size_t N, size_t M> void myMethod(int (&inTab)[N][M]). -
@SingerOfTheFall:这很有意义:它是一个指向可写数组的常量数组。
-
不过,你还是得给这个方法传递一个连续的数据块吧?
-
@MikeSeymour,嗯,我认为
where the argument is a const array of fixed size const array的意思是“常量数组的常量数组”...
标签: c++ multidimensional-array constants argument-passing