References to C + + variables---5

Source: Internet
Author: User

Original blog: Reprint please indicate the source: http://www.cnblogs.com/zxouxuewei/

1. Main use of reference variables:

As a formal parameter of a function, the function uses the original data instead of its copy by using the reference variable as a parameter.

2. Reference variable Usage

Create a reference to a variable:int& a = B; (to declare the type of a as int&, which is a reference to an int variable )

Note: must be initialized when declaring a reference to a variable (as in the previous example)

int A;
int& b;
A = b; Should not does this

3. References can only be set by an initialization declaration and cannot be set by assigning a value

(1) int& a = B; equivalent to int* const A = &b;

(2) The reference can only be set by initialization and cannot be set by assigning a value

int B = + ;
int& a = b;
int C = + ;
A= C; This can change the B's value to 50

4. Use a reference as a function parameter

(1) Reference passing: Use a reference as a function parameter so that the variable name in the function is the alias of the variable in the calling program.

(2) Passing by value and by reference:

/*Pass by value: 2 variables, 2 names*/
voidSneezy (int x)
int Main
{
int times = ; Create a Times variable, assign a value of 20 to it
Sneezy (times);
...
}

void sneezy (int x)
{
... // Create an X variable and assign it the value 20 passed
}
/*Pass by reference: A variable, 2 names*/
voidSneezy (IntX
int Main
{
int times = ; Create a Times variable, assign a value of 20 to it
Sneezy (times);
...
}

void sneezy (int& x)
{
... // make X an alias for times
}

(3) Constant reference

If the programmer's intention is to have the function use the information passed to it without modifying the information and to use the reference, then use a constant reference.

defined in the following way:

double refcube (const double& ra);

(4) Temporary variables, reference parameters, and const

C + + generates temporary variables only if the argument is a const reference and if the actual participating reference parameter does not match.

Two cases:

A. The type of the argument is correct, but not an lvalue.

lvalue Parameters : Data objects that can be referenced, such as: variables, array elements, struct members, references, and dereferenced pointers. non-lvalue include: Literal constants and expressions that contain the polynomial

B. The type of the argument is incorrect, but can be converted to the correct type.

Note : If the intent of a function that accepts a reference parameter is to modify a variable passed as a parameter, creating a temporary variable will block the implementation of this intent.

(5) Use const as far as possible

A. Use const to avoid programming errors that unintentionally modify data;

B. Use cosnt to enable functions to handle const and non-const arguments, otherwise only non-const data can be accepted;

C. Use a const reference to enable the function to correctly build and use temporary variables.

5. Using references for structures

References are well suited for structs and classes. The purpose of introducing references is primarily for these types.

The following code is available:

struct sysop
{
...
int used;
}

Const sysop& use (sysop& sysopref)

Typically, the return mechanism copies the return value into a temporary storage area, which is then accessed by the calling program. A return reference means that the calling program accesses the return value directly.

The const means that you cannot directly modify the structure that it points to by using the returned reference.

Use (looper). Used = ten; can not does this

6. What is the reference, pointer, and by-value pass to use?

(1) The main case of using reference parameters:

A. The programmer can modify the data object in the calling function;

B. By passing a reference instead of the entire data object, you can increase the speed at which the program runs.

(2) For functions that use passed values without modification:

A. Data objects are small, such as built-in data types or small structures, and are passed by value;

B. The data object is data, using the const pointer;

C. The data object is a larger structure, using a const pointer or a const reference;

D. The data object is a class object, using a const reference.

(3) For modifying functions that call data in a function:

A. The data object is a built-in data type, using pointers;

B. The data object is an array, using pointers;

C. A data object is a structure, using pointers or references

D. The data object is a class object, using a reference.

References to C + + variables---5

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.