References are special pointers.

Source: Internet
Author: User

Many people regard pointer and reference as two completely different things. Reference is just an alias and does not occupy the actual memory, but operations are a bit like pointers. in fact, reference is a pointer, but it is a special pointer. the compiler has done some special processing on it. it also occupies memory. however, because the compiler does some special processing, you cannot use & to retrieve the address. The value obtained with sizeof is not 4 or 8, but the size of the referenced object.

As a matter of fact, we also know that if the reference is really just an alias, it doesn't mean anything else. it's so full that we can make a reference. simply use the original name. Why do you use its alias. therefore, the reference only saves an address value.

To distinguish between pointers and references, let's talk about two concepts: constant pointers and pointer constants.

Constant pointer and pointer constant
I think these two terms are very confusing and confusing. If you don't care about them, you can simply see them in English.

Constant pointer (pointer to constant)
I think the translation into a constant pointer is not very good. It should be said that the pointer to the constant, although a few more words but the meaning is much clearer. however, it is not accurate enough. let's give a simple example.

Const int num = 88;

Const int * p = & num; // p is a constant pointer. Does it point to a constant num. Does the constant pointer have to point to a constant? Not necessarily

Int no = 99;

Const int * pp = & no; // pp is also a constant pointer, but it does not point to a constant.

A constant pointer only indicates that the value of the object to which the Pointer Points cannot be changed through a pointer.

Therefore, * p = 123 and * pp = 123 do not work in this way, but no = 123 does.

Constant pointer)
In fact, I think it is better to translate it into a constant pointer. constant is a modifier in front of me.

Int num = 44;

Int * const pp = & num; // You can also enter int const * pp = & num;

A pointer Constant indicates that the pointer cannot point to another place after it points to a certain place.

So if int no = 55;

Pp = & no; // here an error occurs, and pp cannot point to no

We usually need to initialize constants immediately when defining them. The same is true for pointer constants.

Int * pp; // Yes

Int * const pp; // This will not work. However, you can assign a null value. For example, int * const pp = 0;

We only need to check whether the const is on the left or right side of the type keyword. For example, if the above is on the left side of the int, It is the variable pointed to. If it is on the right side of the int, It is the modifier pointer itself.

If both const use const int * const pp = & num; and it is a constant pointer constant, this name is really ugly. I think it is better to call a constant pointer to a constant.

Reference is a special pointer constant.
After talking about the above two concepts, we can easily understand the references.

A reference is actually a constant poniter. It is only a special pointer constant. The syntax is different when used first.

Int num = 22;

Int & rNum = num; // if it is const int num; then the reference is const int & rNum = num;

In addition, the object to be referenced must be valid and cannot be a null reference. in addition, it is no different from pointer constants. A sample. it is said that the de-compilation is used to look at the assembly code, and the final implementation of the reference is the same as that of the pointer. but I did not verify it.

Because the compiler performs special processing, the reference will directly use the name, without adding a star * To unreference. this makes it much easier to use. so where pointer constants are used, they can be replaced by references.

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.