[C ++] Returns an array of pointers

  The array can not be copied, so the function can not return an array. However, the function may return an array of pointers or references. The following is an array of pointers or references returned by:

typedef int ARRT [ 10 ];         // ARRT is a type of alias, type 10 represents the array of integers containing 
the using ARRT = int [ 10 ];           // equivalent statement of ARRT 
ARRT * FUNC ( int I);            // func returns a pointer to an integer array containing 10 of

Wherein arrT alias array containing 10 integers, because we can not return an array, it will return a pointer to an array type definition. Thus, func function accepts an int argument and returns a pointer to a array of 10 integers.

 

Declare a pointer to a function returns an array

int ARR [ 10 ];         // ARR containing an array of 10 integers 
int * PI [ 10 ];         // P1 is an array containing pointers 10 
int (* P2) [ 10 ] = & ARR;     // P2 is a pointer to an integer array containing 10 of

 

These statements are the same dimensions and, if we want to define a pointer to a function returns an array, the array must be followed after the function name. However, the function parameter list is also behind the function name and parameter list should precede the dimension of the array. Thus, the function returns a pointer to an array of the form shown below:

Type (*function(parameter_list))[dimension]

An array of other similar statements, Type indicates the type of element, dimension represents the size of the array. (* Function (patameter_list)) must be present at both ends of the brackets, as we define both ends of the brackets must be the same as when p2. Without this bracket, the return type of the function is an array of pointers.

  By way of specific example, the following statement does not use the function func type alias:

  int (*func(int i))[10];

You can follow in order to understand the meaning of the declaration layer by layer:

  • func (int I) expressed the need for a type of int func argument when calling the function.
  • (* Func (int i)) means that we can perform operations on the reference solution to the result of the function call.
  • (* Func (int i)) [10] represents dereferencing func call will be a size of the array 10.
  • int (* func (int i)) [10] represents the elements in the array of type int.

 Using the return type opposite end of

  Another method can be simplified in the above-stated func new standard C ++ 11, opposite the end of the return type is used (trailing return type). Define any function can be used to return the end opposite, but this form is most effective return type of more complex functions, such as the return type is a reference or a pointer to an array of arrays. In the opposite end of the return type with a parameter list and back - at the beginning of the symbol>. In order to express the true function return type follows the parameter list, we should appear to place a return type of place auto in this:

auto func(int i)->int(*)[10];

Because we return type of the function parameter list in the following form, it can be clearly seen func function returns a pointer, and a pointer to the array containing 10 integers.

Use decltype

  In another case, if we know the function returns a pointer which will point to an array, you can use decltype keyword to declare the return type. For example, the following function returns a pointer, which points to the two known different arrays according to the parameter i is one:

int ODD [] = { . 1 , . 3 , . 5 , . 7 , . 9 };
 int the even [] = { 0 , 2 , . 4 , . 6 , . 8 };
 // return a pointer that points to an array of integers containing 5 
decltype (ODD) * arrPtr ( int I) 
{ 
      return (% I 2 ) & ODD:? & the even;         
}

arrPtr keyword decltype return type is represented by a pointer, the pointer and the object is consistent with the type of odd. Because the array is odd, so arrPtr returns a pointer to the array containing five integers. There is a place to note: The result is an array of decltype, in order to express arrPtr returns a pointer must also add a * symbol in the function declaration.

Guess you like

Origin www.cnblogs.com/lightmonster/p/10993977.html