C ++ summary -- Reference

Source: Internet
Author: User

Thinking in C ++:

Reference (&) is like a constant pointer that can be reversed by the compiler. Parameters used for Functions

The return values of a number table and a function can also be used independently. For example:

Int X;

Int & R = X;

When creating a reference, the reference must be initialized to an existing object, but it can also:

Int & Q = 12;

Here, the compiler assigns a storage unit whose initial value is 12. The reference must be associated with the storage unit

System, which is the storage unit to access when accessing the reference. For example:

Int x = 0;

Int & A = X;

A ++;

In fact, it is to add X. The simplest way to consider referencing is to treat it as a strange pointer.

The advantage of a needle is that you do not have to doubt whether it is initialized (the compiler forces it to initialize), and you do not have to know how it works.

Reverse references to him (this is done by the compiler ).

When using a reference, there are certain rules:

■ When a reference is created, it must be initialized. (Pointers can be initialized at any time)

■ Once a reference is initialized to point to an object, it cannot be changed to another object's reference

. (Pointers can point to another object at any time)

■ A null application is not allowed. You must ensure that the reference is associated with a valid storage unit.

Reference in the function:

The most common reference is the function parameter and return value. When the reference is used as a function parameter, any changes to the reference in the function will change the parameters outside the function. If a reference is returned in a function, it must be treated as a pointer from the function type.

Pointer Reference:

In C, if you want to change the pointer itself rather than the content it points to, the function declaration may look like this:

Void fun (INT **);

When passing it, it must be the pointer address:

Int L = 47;

Int * P = & L;

Fun (& P );

For references in C ++, the syntax is much clearer, and function parameters become pointer references, so you do not need to obtain the pointer address.

# Include <iostream. h>

Void increment (int * & I) {I ++ ;}

Main ()

{

Int * I = 0;

Cout <"I =" <I <Endl;

Increment (I );

Cout <"I =" <I <Endl;

}

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.