C ++ basic tutorial-Reference

Source: Internet
Author: User

References are similar to pointers. They operate on the variable address directly. The difference is that the referenced object cannot be changed. References must be intuitively understood. This tutorial will introduce references to you.

1. Start Geany

1) Click "Application-programming-Geany" in the menu to start Geany and create a c ++ source program;

2) Click the "file-save as" command and use "refer" as the file name to save the file to your folder;

2. Create a reference

1) reference an alias equivalent to a variable or another nickname. It is not a variable, so only the declaration is not defined;

Enter the following code:

Int a = 25;

Int & ra =;

Cout <"ra =" <ra;

2) The first sentence defines an integer variable a, which has an address in the memory and can be accessed with variable name,

The second sentence creates a reference. The type is integer, And the number & is the definition symbol. The previous type must be declaration or definition,

The third sentence is to display ra, which is similar to the pointer. After definition, you can directly use the name ra;

Output ra is actually Output a, for example, variable a has another address name;

3) Next let's look at the relationship between the two, and continue to enter the following code;

A = 16;

Cout <"ra =" <ra <endl;

Ra = 20;

Cout <"a =" <a <endl;

4) The first sentence changes the value of variable a, and ra also changes,

The second sentence changes the reference value of ra, and a also changes;

# Include <iostream>

Using namespace std;

Int main (int argc, char ** argv)

{

Int a = 25;

Int & ra =;

Cout <"ra =" <ra;

Cout <endl;

A = 16;

Cout <"ra =" <ra <endl;

Ra = 20;

Cout <"a =" <a <endl;

Return 0;

}

5) References and pointers are used in many functions. When a function is running, you must use the form parameter. The form parameter is a local temporary variable,

When the function ends, the calculation result is returned. This value must be stored in a local temporary variable,

Using the address features of references and pointers, you can skip local variables and directly operate the passed variables to increase the speed and save memory resources;

6) create a new c ++ program, save the file name as swap, and enter the following code;

# Include <iostream>

Using namespace std;

Void swap (int & rx, int & ry); // declare a function, defined outside the main function

Int main (int argc, char ** argv) // The main function that calls the swap function.

{

Int a = 25;

Int B = 16;

Cout <"a =" <;

Cout <"B =" <B;

Cout <endl;

Swap (a, B); // call a function, directly exchange a, B

Cout <"a =" <;

Cout <"B =" <B;

Return 0;

}

Void swap (int & rx, int & ry) // defines the previously declared function

{

Int temp = rx;

Rx = ry; // use the reference name directly when using

Ry = 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.