C + + supplements (iii)--functions

Source: Internet
Author: User

Definition of a function

  • C + + is a static strongly typed language that, for each function call, checks for the fact that the argument must be the same as the formal parameter type or can be converted to that type.

Parameter passing

  • Parameters of a generic non-reference type are initialized by copying the corresponding arguments. Reference parameters are directly associated with the bound object, not a copy of those objects.
  • Another use of reference parameters is to return additional results to the keynote function (when a return value is insufficient).
  • In love. When a function passes a large object, a reference parameter is used to avoid the copy operation: the formal parameter should be defined as a const reference at this time.
  • Note that when you return a reference, you must not return a reference to a local variable. Also, you cannot return a pointer to a local object.

inline functions

  • Defining a function as an inline function is to "inline" on each call point in the program to avoid the overhead of function calls.
  • Inline functions should be defined in the header file. Its definition can occur more than once, as long as it appears only once in a source file and exactly the same in all files.

Overloaded functions

  • A function cannot implement overloading only on the basis of a different return type. Whether a parameter is const is affected only when the reference or pointer is a parameter.
  • The overload of the function is determined in three steps: The candidate function, select the feasible function, find the best match.
  • To determine the best match, the compiler divides the conversion of the argument type into the corresponding parameter type in descending order: exact matching, matching by type promotion implementation, matching through a standard transformation implementation, and matching by class type conversion.

C + + supplements (iii)--functions

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.