我有点困惑.有两种方法可以从方法返回数组.第一个建议如下:
typedef int arrT[10];
arrT *func(int i);
Run Code Online (Sandbox Code Playgroud)
但是,如何捕获返回的int(*)[]?
另一种方法是通过引用或指针:
int (*func(int i)[10];
Run Code Online (Sandbox Code Playgroud)
要么
int (&func(int i)[10];
Run Code Online (Sandbox Code Playgroud)
返回类型是int(*)[]或int(&)[].
我遇到的麻烦是如何分配一个变量来接受这个点,我继续得到如下错误:
can't convert int* to int (*)[]
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么或者我的知识缺乏什么?
如果要按值返回数组,请将其放在结构中.
标准委员会已经这样做了,因此你可以使用std::array<int,10>.
std::array<int,10> func(int i);
std::array<int,10> x = func(77);
Run Code Online (Sandbox Code Playgroud)
这使得通过引用返回非常简单:
std::array<int,10>& func2(int i);
std::array<int,10>& y = func2(5);
Run Code Online (Sandbox Code Playgroud)