Article 1: Careful distinction between pointers and references

Source: Internet
Author: User
Pointers and references look very different, pointers use the * and-I operators, and references use the. operator, but they seem to do something similar.
First, the requirement is confirmed that C + + has no null reference (null reference), that a reference must always represent an object, so it cannot point to NULL, cannot declare a variable without assigning an initial value, and cannot change its point (in fact, to use a reference more appropriately) in the process. If the above conditions are not met, consider using pointers.
Using a reference may be more efficient than a pointer, because the reference does not need to check for empty conditions.
There are also situations where references need to be used, such as when implementing certain operators. The most common case is [], this operator needs to return something that "can be used as a assignment assignment object".
e.g.:vector<int> V (10);
v[5]=10;//This is a reference, if the pointer is the words should be *v[5]=10;

Article 1: Careful distinction between pointers and references

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.