Basics: Arrays cannot be copied, functions cannot return arrays, only pointers or references to arrays are returned.
A method that defines a function that returns an array pointer , taking as an example a function that receives a parameter as a reference to an array containing 10 integral elements and a pointer to an array containing 10 integral elements.
Law one: (do not use aliases)
int(*getcopyarr (int(&arr) [Ten]))[Ten]{ int(*n) [Ten] = (int(*) [Ten])New int[Ten]; for(inti =0; I <Ten; i++) (*N) [I] =Arr[i]; returnN;}//Note: The function returns an array allocated in the heap, and if it does not release memory after the array is obtained, it will cause a memory leak, which is only as an example .
Law II:
// tail return type (c++11 standard)auto getCopyArr2 (int(&arr) [ten]),int(*) [ ]{ return Getarr (arr);}
Fahsarm
// Use type aliases (1) int arrt[];arrt* GETCOPYARR3 (int(&arr) [ten]) { return Getarr (arr);}
Law IV:
// Use type aliases (2) (C++11 standard) using int [];arrt2* GETCOPYARR4 (int(&arr) [ten]) { return Getarr (arr);}
A method that declares a function pointer to a function that returns an array pointer :
int (* (*GETCA) (int(&) [ten])) [ten= Getarr;
// A function pointer int (*fun) (int, int) that resembles the int sum (int a, int b) function // .
Reference blog post and "C + + Primer 5th Edition"
---------------------------------------------------------------------------------------
Finally, here are two examples of "refreshing the Brain":
// declares a pointer to an array of 10 elements, where each element is a function pointer, and the return value of the function is int, and the argument is int* int (* (*p) [ten]) (int *) // or int (* (*p[)) (int *) // Source
// Defines a pointer to an array of 4 elements, each of which is a pointer to a function whose reference returns a pointer to an array of 10 elements with a value of 10 elements int (* (*getca[4]) (int(&) [ten])) [ten];
c++--functions that return array pointers