C ++ overload, pointer, and reference

Source: Internet
Author: User

C ++ overload, pointer, and reference
C ++ overload/pointer/reference 1. Overload

Function names are the same and parameters are different (type and quantity ). Different return values cannot be overloaded.

As follows:

int add(int a, int b){    cout<<"add int+int"<
  
2. pointer

Same as C Language

3. Reference 3.1. What is reference

Referencing is the alias of an object, which is the same as a person's name: there is a name on the ID card, and others can give you multiple small names that are frequently used in daily life.

3.2. The referenced Representation Method

The expression of the definition reference is similar to the definition pointer, but it is replaced *.The reference is just an object alias and does not allocate memory space separately.The referenced operation is the same as the variable operation. The format is: Type & reference variable name = variable names that have been defined.

3.3 references

A. It must be initialized during the Declaration and cannot be changed later.

B. You cannot create an array reference. Because an array is a collection composed of several elements, you cannot create an array alias.

C. Find the address of the reference, that is, find the address of the target variable.

Example 1:

Int I = 5; int j = 6; int & k = I; k = j; // The values of k and I are changed to 6;

Example 2: Reference Transfer

Void Swap (int & left, int & right) // if a reference is used, no temporary copy is made. & the usage instructions here are just another name of the original parameter, therefore, the variable value is directly modified based on the original parameter. {Int temp = left; right = left; left = temp ;}

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.