从函数返回多维数组
如何返回存储在类的 private
字段中的多维数组?
class Myclass {
private:
int myarray[5][5];
public:
int **get_array();
};
// This does not work:
int **Myclass::get_array() {
return myarray;
}
我收到以下错误:
无法将
int (*)[5][5]
转换为int**
作为返回
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
二维数组不会衰减为指向整数的指针。它衰减为指向整数数组的指针 - 也就是说,只有第一个维度衰减为指针。该指针不指向 int 指针(该指针在递增时会增加指针的大小),而是指向 5 个整数的数组。
当每个子数组单独分配时(也就是说,你最初有一个指针数组),使用指向指针的指针(
int**
)这里我们有一个由五个指针组成的数组,每个指针都指向第一个item在一个单独的内存块中,总共6个不同的内存块。
在二维数组中,您将获得单个连续的内存块:
您应该看到这些东西的内存布局完全不同,因此这些东西不能以相同的方式返回和传递。
A two-dimensional array does not decay to a pointer to pointer to ints. It decays to a pointer to arrays of ints - that is, only the first dimension decays to a pointer. The pointer does not point to int pointers, which when incremented advance by the size of a pointer, but to arrays of 5 integers.
A pointer to pointer (
int**
) is used when each subarray is allocated separately (that is, you originally have an array of pointers)Here we have an array of five pointers, each pointing to the first item in a separate memory block, altogether 6 distinct memory blocks.
In a two-dimensional array you get a single contiguous block of memory:
You should see that the memory layout of these things are completely different, and therefore these things cannot be returned and passed the same way.
您可以返回两种可能的类型来提供对内部数组的访问。旧的 C 风格将返回
int *[5]
,因为数组很容易衰减为指向第一个元素的指针,该元素的类型为int[5]
。现在,您还可以返回对内部数组的正确引用,最简单的语法是通过 typedef:
或者没有 typedef 会更麻烦一些:
C++ 版本的优点是保留实际类型,这意味着数组的实际大小在调用方是已知的。
There are two possible types that you can return to provide access to your internal array. The old C style would be returning
int *[5]
, as the array will easily decay into a pointer to the first element, which is of typeint[5]
.Now, you can also return a proper reference to the internal array, the simplest syntax would be through a typedef:
Or a little more cumbersome without the typedef:
The advantage of the C++ version is that the actual type is maintained, and that means that the actual size of the array is known at the callers side.
要返回指向数组成员数组的指针,所需的类型是
int (*)[5]
,而不是int **
:To return a pointer to your array of array member, the type needed is
int (*)[5]
, notint **
:如果它应该被隐藏,为什么你首先要归还它?
无论如何,您不能从函数返回数组,但可以返回指向第一个元素的指针。 5x5 整数数组的第一个元素是什么?当然,是 5 个整数的数组:
或者,您可以通过引用返回整个数组:
...欢迎来到 C 声明符语法地狱 ;-)
我可以建议
std::vector 吗? >
或boost::multi_array
代替?If it's supposed to be hidden, why are you returning it in the first place?
Anyway, you cannot return arrays from functions, but you can return a pointer to the first element. What is the first element of a 5x5 array of ints? An array of 5 ints, of course:
Alternatively, you could return the entire array by reference:
...welcome to C declarator syntax hell ;-)
May I suggest
std::vector<std::vector<int> >
orboost::multi_array<int, 2>
instead?更简单的是
decltype(auto)
(C++14 起)然后
decltype
(C++11 起) (成员应该在方法之前声明)然后 typedef 方式:
因为常规语法非常难看:
使用
std::array, 5> (C++11 起) 将具有更自然的语法。
Simpler would be
decltype(auto)
(since C++14)then
decltype
(since C++11) (member should be declared before the method though)then typedef way:
as regular syntax is very ugly way:
Using
std::array<std::array<int, 5>, 5>
(since C++11) would have more natural syntax.我设法使用自动类型推导使这个函数在 C++0x 中工作。但是,如果没有它,我就无法让它工作。 C++ 中对本机 C 数组的支持不是很好 - 它们的语法极其丑陋。您应该使用包装类。
这段代码编译得很好。您可以在 Boost (boost::array) 中获取预先编写的包装类,以支持整个 shebang。
I managed to make this function work in C++0x using automatic type deduction. However, I can't make it work without that. Native C arrays are not supported very well in C++ - their syntax is exceedingly hideous. You should use a wrapper class.
This code compiles just fine. You can get pre-written wrapper class inside Boost (boost::array) that supports the whole shebang.