References and pointers

Source: Internet
Author: User

A reference is an alias, which is essentially a pointer, and the implementation of a reference is simply a constant pointer to a particular object. The object is manipulated directly when referencing a parameter, reducing the cost of copying when the value is passed.

So the copy constructor parameter type is (const type & ...), and by the way copy is constructed to copy the object in a custom manner, avoiding the compiler's shallow copy behavior.

So what are the similarities and differences between pointers and references:

1, the reference must be initialized; pointers are not required (the compiler will not error, but this behavior will be hidden);

2, the reference is an alias, its size is related to the referenced object, and the pointer is an address, the size of the same (32-bit system, 64-bit system);

3. After the reference is initialized, the referenced object cannot be changed, and the pointer can change to point to the object;

4, pointers with pointers, that is, two-level pointers, but no reference;

5, no reference pointer; But a pointer reference, such as int*& &, is int*, and the effect is equivalent to two level pointers;

6, an array of pointers, such as char* arr[]; There is no reference to the array, but there is an array of references;

References and pointers

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.