C++:const and References __c++

Source: Internet
Author: User
Tags int size

The above blog we have introduced the basic const, for this blog,

first, what is a reference:

A reference is a different name for the object, and a reference type refers to another type.

How to define:

To define a reference type by writing the declaration as a &d, where D is the declared variable name,

int size =
&sum int = size;

To be aware of:

we have to assign a value to a reference when we reference it,

For example:

int &size;

This way is not possible, in general initialization variables, the initial value is copied to the newly created object, and when the reference is defined, the program binds the reference to its initial value, rather than copying it, once the initialization is complete, the reference and his initial value are bound together.

We need to remember this passage:

a reference is an alias to an object, so the reference is not an object, but an alias to the object. After a reference is defined, all actions are made on the object that references the binding.

So here's the question:

Can we define references for reference? The answer, of course, is no. Because we say that the reference just gives the object a different name, the reference is not an object, so we can't do that,

Of course we can bind many references to objects.

In addition, the value of the note is:

All other types of references and objects to bind are strictly matched, and references can only be bound to objects, not to literal values or to the evaluation of an expression.

Well, the basics of the quote are done, so.

See first const and reference:

We can bind a reference to a const object that is bound in the same way that it is bound to another object, called a constant reference, and unlike a normal reference, a reference to a constant cannot be used to modify the object to which it is bound

For example:

const int CI = +
const int &r1 = CI;  //correct, references and objects are constants
r1 = 11//This is wrong because R1 is a reference to a constant
int &r2 = CI/ /Wrong. Cannot have a very reference to a constant object;

As we said above, the type of reference must be consistent with the type that you are pointing to. But there are two exceptions. The first exception is to allow an arbitrary expression to be used as an initial value when the constant reference is initialized, as long as the result of the expression can be converted to a reference type, in particular, to allow a constant reference to a bound object of a very quantity, Literal value or even a general expression.

such as:

int i=;
const int &R1 = i;
const int &R2 = n,
const int &R3 = r1 * 2;

these are all legal.

So the question is, what happens when the type is inconsistent?

First of all:

Double dval = 3.14;
const int & r1 = Dval;

Here R1 is an int reference, but Dval is a double, so to make sure that R1 binds an object, the compiler converts the above code to:

const int TEMP = dval
const int &R1 = temp;

In this case R1 binds a temporary amount.

So what happens if R1 is not a constant?

If R1 is not a constant, allows assigning values to R1, so that the value of the object referenced by the R1 is changed, but at this point the bound is a temporary amount, rather than dval, since we will R1 reference Dval, so long certainly the same R1 change dval value, otherwise why give R1 assign value it.

So C + + stipulates that this behavior is illegal.


Related Article

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.