When should I use pointers and references?

Source: Internet
Author: User

References are a new type introduced by C ++. Therefore, the semantics of C ++ references poses a headache for many new users.

A reference is the alias of an object. When a variable is referenced, the effect is directly reflected in the variable.

The reference must be initialized during declaration. Otherwise, the compiler will prompt an error, so the reference must not be empty.

Int I = 20;

Int & J = I;

Int & K; // error. It must be initialized.

++ J; // reference J itself remains unchanged, and I ++ is executed.

Extern Int & K; // OK, where k is defined

In addition, if you perform the address fetch operation on the reference, the address of the object to which the reference is returned. In general, the compiler does not allocate space for reference. When the compiler is compiling, the reference is displayed, but a mark is added to the symbol table.

When referenced as a Member member of the class, the compiler will allocate space for it.

The life cycle of the referenced object is the life cycle of the referenced object. If an object is invalid, its reference will become invalid. Therefore, it is dangerous to return a reference to a local object in a function. For example:

Int & func ()


Int I = 20;

Int & K = I;

Return K; // return the reference of a local object. After the function is called, The I used by K is invalid.


I remember that I encountered such an interview question during the previous interview:

What do int * & and Int & * mean respectively?

Sorry, I did not answer this question for the first time. The correct answer is:

Int * & declares a reference to the int pointer, and Int & * is not a legal statement. The pointer to the reference is invalid because the reference does not occupy memory at all.


Pointer semantics is much simpler than references. Return to the title of this article. Under what circumstances should I use a reference, and under what circumstances should I use a pointer?

Because the referenced object cannot be modified once it is initialized, if you know that it can always point to an object, once it points to an object, you can select reference when you do not need to modify it. If it is possible to point to or not to, use a pointer.


In addition, references are generally used to pass parameters to prevent overhead caused by object copying. We recommend that you use references when passing object parameters.

Also, define the [] operator for the container and return a reference. At this time, the returned pointer is always strange.


Since pointers and references have different semantics, you must understand the differences in semantics to avoid unnecessary troubles.







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.