C + + must Know (v) references are aliases rather than pointers

Source: Internet
Author: User
Tags aliases

A reference (reference) is an alias to an existing object. After the reference is initialized with an object, the name of the object or the name of the reference can be used to point to the object (refer to):

int a = 12;
int &ra = a;
--ra; // a == 11;
a = 10; // ra = 10;
int *ip = &ra;

People often confuse references with pointers, probably because the C + + compiler usually implements the reference in the form of pointers, but the reference is not a pointer, and the behavior is not the same as the pointer.

There are three major differences between references and pointers: first, there is no null reference (null Reference); Second, all references are initialized; third, a reference always points to the object used to initialize it. For example, in the previous example, the reference RA has been pointing to a for the entire life period. Most misuse of citations breeds understanding of the three major differences.

Some compilers can capture the obvious attempts to create a null reference:

Employee &anEmployee = *static_cast<Employee*>(0);

However, compilation bubbling may not detect a less obvious attempt to create a null reference, resulting in undefined behavior at run time:

Employee *getAnEmployee();
//...
Employee &anEmployee = *getAnEmployee(); // 可能是糟糕的代码
if (&anEmployee == 0)

If Getanemployee returns a null pointer, then the behavior of the subsequent code is undefined. In this example, it is best to use a pointer to hold the result of the Getanemployee return.

Employee *employee = Getanemployee ();

if (employee)//...

The requirement that a reference must be initialized means that when a reference initializes the object it points to must exist. This is important, so I want to repeat it again: a reference is the alias of an object that already exists before the reference is initialized. Once a reference is initialized to point to a particular object, the reference can no longer point to another object, and within the lifetime of a reference, the reference is bound to the object used to initialize it. In fact, after a reference completes its initialization, it is just the alias of the object that initialized it. This "alias" attribute makes references often a good choice for formal parameters. In the following swap function template, formal parameters A and B are aliases that are passed to the arguments of the invocation:

template<typename T>
void swap(T &a, T &b)
{
 T temp(a);
 a = b;
 b = temp;
}
//...
int x = 1, y = 2;
swap(x, y);

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.