Pointers and references

Source: Internet
Author: User

1. A reference is an alias for a variable that has already been defined. Must be initialized at the time of declaration of reference. A reference is closer to a const pointer, and once it is associated with a face, it is loyal to it and cannot be changed (the reference can be set by initialization, but cannot be set by assignment).

2. The difference between a reference and a value is that, in a function, it is worth saying that a temporary variable is created inside the function, but the application does not, because the reference and need to pass in the value is the same value.

3. The difference between the communication and the address, except that they are declared in the function is different, that is, after the address in the function will be used to dereference the operator more than once.

4. Referenced properties: If the programmer's intention is to have the function use the information passed to it instead of modifying the information, and want to use the reference, then use the constant reference,

5. Temporary variables, reference parameters, and const: C + + generates temporary variables if the arguments and reference parameters do not match. C + + is allowed to do so only when the parameter is a const reference. The compiler will generate a temporary variable in the following two scenarios: A. The type of the argument is true, but is not an lvalue. B. The type of the argument is incorrect but can be converted to the correct type. The temporary variable is only present in the function's calling period, after which the compiler arbitrarily deletes it (for detailed reasons, refer to C++primerplus 291 for a const explanation).

6. Use const whenever possible

A. Using const to avoid programming errors that unintentionally modify data

B. Using const enables a function to handle const and non-const arguments, and no person can accept data that is not const.

C.const allows the function to generate correctly and use temporary variables

When to use reference parameters:

There are two main reasons for using reference parameters

1. Ability to modify data objects in functions that call I

2. You can increase the speed of your program by passing a reference instead of the entire data object

But when the data objects are large (structs and classes), the second reason is important. These are also the reasons for using pointer parameters. This makes sense because the reference parameter is actually another interface to the code of the opportunity pointer. So when should I use a reference and when should I use a pointer? When should we use the value of the pass? The principles are as follows:

If the object data is small, such as a built-in data type or a small structure, it is passed by value.

If the data object is an array, use the pointer, which is the only option and declare the pointer to a const pointer.

The shareholder data object is a larger structure, which is to use a const pointer or a const reference to improve the efficiency of the program. This saves time and space needed to replicate the construction.

If the data object is a class object, the Cosnt reference is used. The semantics of class design often require the use of references, which is the main reason for the new features of C + +, so the standard way to pass class object parameters is by reference.

For modifying the function that invokes the data in the calling function:

If the data object is a built-in type, the pointer is used. If you see code such as Fixit (&x), where x is an int, it is clear that the function will modify the X

If the number of data Objects array, you can only use pointers.

If the data object is a struct, the reference or pointer is used.

If the data object is a class object, use a reference (refer to C++primerplus)

Pointers and references

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.