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;
}