C + + array types and function types

Source: Internet
Author: User

The reason to take the array type of C + + to the function type is to say, because the two are the same in many places.

First, the declaration is similar in form:

Array type: Type [num] array: type Name[num]

Function type: return_type (formal parameter list) function: Return_type name (formal parameter list)

  

Array pointer type: type (*) [num] array pointer: type (*NAME) [num]

function pointer type: return_type (*) (formal parameter list) function pointer: Return_type (*NAME) (formal parameter list)

  

Array reference type: Type (&) [num] Array reference: Type (&name) [num]

Function Reference type: Return_type (&) (formal parameter list) function reference: Return_type (&name) (formal parameter list)

  

function returns array pointer: type name (parameter list) [num]

function returns function pointer: Return_type (*name (formal parameter table)) (returns the formal parameter list of the function))

  

arrays, functions in common:

1. Arrays, functions are not copied.

2. Because the 1th, the array, the function can not do the function return value, but the function may return the array, the function pointer or the reference.

3. Arrays, functions can be used for function parameters, but because of the 1th, the compiler will handle them.

If the parameter type is an array, the actual parameter type is converted to a pointer of the element type, such as void fun (int arr[5]) equivalent to void fun (int arr*).

If the parameter type is a function, the actual parameter type is converted to the corresponding function pointer type, such as void fun (int test ()), which is equivalent to void fun (int (*test) ()).

  

Note: 1. Array pointer types are legal, but cannot get pointers to arrays, so there is no practical use.

2. The function type is legal, but its defined objects cannot be initialized and assigned, and can only be used to declare function pointers or function reference types.

  

  

C + + array types and function types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.