C + + Reference variables

Source: Internet
Author: User

Reference Variable

use of reference variables in C + +:

Definition:

int rate=80;

int & Pt=rate

1.pt is a reference variable that must be initialized when a variable is referenced. The value of PT and rate points to the same memory variable. The most useful reference is as a function parameter (struct and object). By referencing variables as arguments, you can use the original data instead of the copy.

2. The feature of the reference is closer to the const pointer, which, once associated with a variable, will always be loyal to it, can be initialized to set the reference, but cannot be set by assigning a value

int other=30;

Pt=other;

at this point, the value of rate and the value of PT are changed to 30 but the address is the original rate. Instead of the other address.

3. If the program does not want to change the information passed to it, but wants to use a reference, then you should use a constant reference

int Test (const int & P)//when the P-value is changed, the compiler will generate the wrong message

4. If the arguments and application parameters do not match, C + + will generate temporary variables for this, which is allowed only when the parameter is a const reference, and a temporary variable is generated in the following cases, and the function argument references the variable.

* * * * * * When a parameter is not a const reference, if it is not an lvalue (and cannot generate a temporary variable) it will result in a compilation error * * * * (emphasis on the 3rd, that is, const)

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

the. Argument type is incorrect, but can be converted to the correct type

Lvalue Concept: Data objects that can be referenced in addition to literal constants and expressions that contain a polynomial

5. Try to declare the reference returned by the function as const so that you do not modify its value by returning the reference. The return reference is actually the alias that returns the referenced variable

const sysop & Use (sysop & pref)

sysop pref={};

sysop test=use (pref);

equivalent

User (pref);

Test=pref;

6. Inheritance is characterized by a reference to a base class that can point to a derived class object without forcing a type conversion. The benefit is that you can define a function that accepts a base class reference to a seat parameter, which can either use a base class object as an argument or a derived class as a parameter


principle of Use:

for functions that are passed by value without modification:

. Data objects are small and pass by value

If the data object is an array, a pointer to the const is defined

If the data object is a large structure, you can use the const pointer or the COSNT reference.

if it is a class object, use a const reference.

for functions that modify values

if it is built-in data, use the pointer

. Arrays, you can only use pointers

. Structure using pointers or references

. Object. Using References

C + + reference variable (GO)

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.