C + + const reference, temporary variable reference parameter

Source: Internet
Author: User

C + + references-temporary variables, reference parameters, and const references

If the actual participation reference parameter does not match, C + + generates a temporary variable. If the reference parameter is const, the compiler generates a temporary variable in the following two scenarios:

The argument type is correct, but not an lvalue

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

Lvalue parameters are data objects that can be referenced, for example, variables, array elements, struct members, references, and dereferenced pointers are lvalue, and non-lvalue include literal constants and expressions that contain a polynomial. Define a function

Double Refcube (const double& RA) {         returnra*ra*ra;} double side = 3.0;double* PD = &side;double& rd = side; Long edge = 5l;double lens[4]={2.3,3.4,4.5,6.7};d ouble c1 = Refcube (side);  RA is sidedouble c2 = Refcube (lens[2]); RA is lens[2]double c3 = Refcube (rd);   RA is rddouble c4 = refcube (*PD);  RA is *pddouble c5 = refcube (edge);  RA is a temporary variable double c6 = refcube (7.0);  RA is a temporary variable double c7 = Refcube (side+10.0); RA is a temporary variable


Parameter side lens[2] Rd and *PD are both named, double type data objects, so you can create a reference for it without requiring a temporary variable. However, although the edge is a variable, the type is not correct, and the double reference cannot point to long. On the other hand, the types of parameters 7.0 and side+10.0 are correct, but without names, in which case the compiler will generate a temporary anonymous variable and have the RA point to it. These temporary variables exist only during a function call and can be arbitrarily deleted by the handler compiler

So why is this behavior feasible for constant references, but not in other cases?

Void swapr (int& a,int& B)

{

Inttemp;

Temp=a;

A= b;

b= temp;

}

Under the earlier lax rules of C + +, what happens when you do the following?

Long A = 3,b = 5;

SWAPR (A, b);

The types here do not match, so the compiler creates two temporary int variables, initializes them to 3 and 5, and then swaps the contents of the temporary variables, while a and b remain unchanged

In short, 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, and the workaround is to prohibit the creation of temporary variables, which are the standard of the C + + standards that Zhengyang made,

Now look at the Refcube () function, which is intended to use only the values passed instead of modifying them, so the temporary variable has no adverse effect. Instead, it makes the function more generic in terms of the types of parameters that can be processed. Therefore, if a declaration designates a reference as const,c++ will generate a temporary variable if necessary, in fact, a C + + function that is a const reference to a parameter, if the argument does not match, its behavior is similar to passing by value, in order to ensure that the original data is not modified, a temporary variable is used to store the value,

(PS: If the parameter of a function call is not an lvalue or does not match the type of the corresponding const reference parameter, C + + creates an anonymous variable of the correct type, passes the value of the parameter of the function call to the anonymous variable, and lets the argument refer to the variable)

Whenever possible, use the const

Use COSNT to avoid programming errors that unintentionally modify data

Use const to enable a function to handle const and non-const arguments, otherwise it will only accept non-const data

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

C + + const reference, temporary variable reference parameter

Related Article

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.