C + + Reference

Source: Internet
Author: User

It's a good one.

Http://www.cnblogs.com/Mr-xu/archive/2012/08/07/2626973.html

What do you mean by reference?

A reference is another name for an object.

Main uses:

To describe the parameters and return values of the function, especially the overloads of the operators.

Usage:

X var;

x& r = var;

Then R is a reference to the object Var, and & is here as the reference identifier instead of the zone address.

Characteristics:

(1) A reference must be initialized at the time of creation to ensure that the reference points to an object.

int i = 1;

int& r1 = i; Correct: R1 is initialized

int& R2; Error: R2 was not initialized

extern int& R3; Correct: R3 has been initialized elsewhere

(2) All operations on a reference can be viewed as being performed on the referenced object.

void G ()

{

int II = 0;

int& rr = II;

rr++; Actually executed the ii++.

int* pp = &rr; PP points to the II

}

Therefore, you can completely replace the reference with the referenced object when you manipulate the application.

(3) The value of a reference cannot be changed after initialization, and it always refers to the object it initializes, to the end.

(4) The address of the object referred to by R can be obtained by &r.

(5) The reference itself does not occupy the storage unit, and sometimes the compiler can remove the reference by optimization, so that at execution there is no reference at all.

(6) For t& Such a reference, a variable reference, the referenced type must be the left value of T, and for const t& is a constant reference, the initial type does not have to be an lvalue, or even a type T, at this time:

[1] First, the type must be implicitly converted to type T;

[2] The result is then deposited into a temporary variable of type T;

[3] Finally, the temporary variable as the initial value.

double& dr = 1; Error, asking for left value

Const double& CDR = 1; That's right

The explanation for the initialization is

Double temp = double (1); First create a temporary variable with the correct value

Const double& CDR = temp; Use temporary variables as the initial value of the CDR

This temporary variable that holds the reference initializer will persist until the scope of the reference ends.

(7) The reference to the variable and the reference to the constant need to be distinguished, and the constant reference cannot be modified by reference to the target variable, so that the referenced target is called a const and achieves some security.

string foo ();

void Bar (string& s);

Then the following expressions will be illegal:

Bar (foo ());

Bar ("Hello World");

Because both foo () and "Hello World" strings produce a temporary object, in C + +, these temporary objects are const types, and the expression above attempts to convert a const type object to a non-Congst type object is illegal.

(8) A function parameter can be described by reference so that the function can change the value of the parameter passed in.

void increment (int& aa) {aa++;}

void F ()

{

int x = 1;

Increment (x); x = 2

}

In order to improve the readability of the program, you should avoid allowing the function to modify their parameters. Instead, let the function explicitly return a value, or explicitly require a pointer parameter:

int next (int p) {return p+1;}

void incr (int* p) {(*p) + +;}

void G ()

{

int x = 1;

Increment (x); x = 2

x = Next (x); x = 3

INCR (&X); x = 4

}

Passing the reference in as a parameter will make it strongly felt that the parameter will be modified.

(9) References can also be used to define functions so that they can be used either to the left of the assignment or to the right.

(10) You cannot create a reference to an array because the array is a collection of several elements.

Reference as return value

Reference as return value needs to be added & in front of the function name

The greatest benefit of a reference as a return value is that it does not produce a copy of the returned value in memory.

#include <iostream.h>

float temp;

Float fn1 (float R);

Float &fn2 (float R);

Float fn1 (float R)

{

temp = (float) (r*r*3.14);

return = temp;

}

flaot& fn2 (float R)

{

temp = (float) (r*r*3.14);

return temp;

}

void Main ()

{

float A = FN1 (10.0);

float &b = fn1 (10.0);

float c = fn2 (10.0);

float &d = fn2 (10.0);

cout << a << c << D;

}

A reference to a local variable cannot be returned, mainly because the local variable is destroyed after the function returns, so the returned reference becomes a reference that has no meaning and causes the program to enter an unknown state.

It is not possible to return a memory reference that is allocated inside a function, although there is no problem with passive destruction, and for this case (the use of new allocated memory inside the function returned), there are other embarrassing situations, such as a reference returned by a function that appears only as a temporary variable, without being given a variable of time. The space that this reference points to cannot be released, resulting in memory leak.

You can return a reference to a class member, but it is best to be const, primarily when the object's properties are associated with a business, and its assignment is often related to some other property or object's state, and if a very significant reference is obtained, it can result in a simple assignment that destroys the integrity of the business rule.

References are overloaded with some operators, which are also related to references that can be left-valued.

References and polymorphism

C + + Reference

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.