Differences between references and pointers

Source: Internet
Author: User

References use the "." operator and pointers use the "*" and "->" operators, which are the basic and most familiar differences between the two.

First, there is no so-called null reference. A reference must always represent an object. Therefore, C ++ requires that the references must have an initial value:

String & rs; // error! References must be initialized

String s ("xyzzy ");

String & rs = s; // no problem. rs points to s.

However, pointers does not have such restrictions, although there are great risks:

String * ps; // uninitialized pointer, valid, but highly risky

Although C ++ does not have such restrictions, it is recommended that we initialize a pointer to avoid the occurrence of a wild pointer.

The following situations are also not agreed and should be taboo:

Char * pc = 0; // Set pointer to null

Char & rc = * pc; // Let references represent the return value of null pointer

This is harmful, and the result cannot be pre-defined (C ++). The compiler can generate whatever possible output. So,

We will never consider the possibility that "reference is null.

The above facts mean that using reference may be more efficient than using pointer. This is because you do not need to verify it before using reference:

Void printDouble (const duble & rd)

{

Cout <rd; // You do not need to test rd. It must represent a double.

}

Void printDouble (const double * pd)

{

If (pd) // check whether it is null pointer

   {

Cout <* pd;

   }

}

Second, another important difference between pointers and references is that pointers can be assigned another value, pointing to another object, but references always points to (representing) the object it initially obtained:

String s1 ("Nancy ");

String s2 ("Clancy ");

String & rs = s1; // rs represents s1

String * ps = & s1; // ps indicates s1

Rs = s2; // rs still represents s1, but the value of s1 is now "Clancy"

Ps = & s2; // ps is now directed to s2, and s1 remains unchanged

In other cases, you also need to use references. For example, when you implement some operators, the most common example is operator []. This operator must very specifically return something that "can be taken care of as an assignment value assignment object:

Vector <int> v (10); // generates an int vector with a size of 10.

V [5] = 10; // The value assignment object of assignment is the return value of operator []

Therefore, when you know that you need to point to something and never change to something else, or when you implement an operator and its syntax requirements cannot be fulfilled by pointers, you should choose references. Use pointers whenever you want.

Differences between 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.