References in C ++

Source: Internet
Author: User

Anyone who has learned C language knows that to learn C language well must learn pointers. pointers can be said to be an address in the memory, but in C ++, pointers do not seem to be so prominent, the usage of pointers poses many problems. Therefore, a reference is added to C ++, which is indicated by "&" in the Code. It is the same as the symbol used to obtain the address value, of course, when learning to use it, we must be aware of the differences between the symbols for referencing and getting the address value.
A reference is an alias for a variable. References in C ++ are divided into the following three types:

 

The following describes the specific code examples:

1. Reference to variables

Format: Data Type & alias = variable name defined

Instance:

# Include <iostream. h>

Void main ()

{

Int m;

Int & n = m; // alias for variable I reference j

M = 30;

Cout <"m =" <m <"n =" <n <"\ n ";

N = 80;

Cout <"m =" <m <"n =" <n <"\ n ";

Cout <"Address of m" <& m <"\ n"; // obtain the m Address, cout <"Address of n" <& n <"\ n ";

}

Running result:

 

The running results show that, after the alias n is referenced in the Variable m, m is assigned a value, that is, n is assigned, and n is assigned a value to m, in addition, we found that the address values of m and n are the same, so the reference is to alias the variable, and no new memory space is applied.

However, pay attention to the following issues when using references:

(1) when defining a reference, it must be initialized immediately and cannot be assigned a value later.

(2) The reference is actually an implicit pointer and can be used less than "*".

(3) The reference cannot be re-assigned or used as an alias for another variable.

(4) You cannot create a pointer to a reference, create a reference, or create a reference array.


2. Reference to function parameters

Instance code:

# Include <iostream. h>

Void swap (int & m, int & n)

{

Int temp;

Temp = m;

M = n;

N = temp;

}

Void main ()

{

Int a = 2, B = 4;

Cout <"a =" <a <"B =" <B <endl;

Swap (a, B );

Cout <"a =" <a <"B =" <endl;

}

Running result:

 

Through code and running results, we can find that when using reference as a function parameter, the address is passed, not the value. The method of referencing the parameter can replace the method of calling the pointer address in C language.


3. function return value

Instance code:

# Include <iostream. h>

Int a [] = {1, 3, 5, 7, 9 };

Int & index (int); // declare the referenced Function

Void main ()

{

Index (2) = 25; // assign a value to a [2]

Cout <index (2) <endl;

}

Int & index (int I)

{

Return a [I];

}

Running result:


The running result shows that the 3rd elements in array a [] are changed to 25.
Well, I will introduce it here, hoping to help you.

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.