Deep differences between pointers and references

Source: Internet
Author: User

In C ++, pointers and references seem to be somewhat different, but they do similar things. So what are the different things between them?

First, you must recognize a null reference. A reference must always represent an object.

If a variable is used to point to (represent) another object, but it may not point to (represent) any object, then the pointer should be used ), because pointer can be set to null. In other words, if the variable always represents an object, that is, if the variable is not allowed to be null, reference should be used at this time.


For example:

Char * pc = 0; // set the pointer pc to null

Char & rc = * pc; // Let the reference (rc) represent the value pointed to by the null pointer (pc)


The results will generate unexpected results, and the compiler will generate any possible output. The people who write this code should be isolated from the public ...., it is not guaranteed that similar behaviors do not exist.

Remember that using reference means not null.


Since the reference must represent an object, c ++ requires that the reference must have an initial value.


String & rs; // error reference must be initialized.

String s ("abcd ");

String & rs = s; // OK, rs points to s;


However, pointers do not have these limitations.

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

Without the so-called null reference, it means that using the reference may be more efficient than the pointer, because you do not need to test its validity before using the reference.


For example:

Void printDouble (const double & rd)


{


Cout <rd; // you do not need to test rd. It represents a double.


}


If pointer is used, you have to test whether it is null.

Void printDouble (const double * pd)

{


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

{

Cout <* pd;


}


}


 

Second, another important difference between pointer and reference is that pointer can be re-assigned and pointed to another object, but the reference always points to the object initially obtained.

String s1 ("abc ");

String s2 ("123 ");


String & rs = s1; // rs represents s1.


String * ps = & s1; // ps points to s1.


Rs = s2; // rs still indicates s1, but the value of s1 is now "123 ".

Ps = & s2; // ps is directed to s2, but S1.


Generally, a pointer is used when you need to consider the possibility of "not pointing to any object", or when "pointing to different objects at different times. In the previous case, the pointer can be set to null, and in the latter case, the object referred to by the pointer can be changed. When "It always represents an object" and "once it represents this object, it cannot be changed", then reference should be used.


There is another situation that needs to be referenced. For example, when some operators are implemented, the most commonly used operator []. this operator must return something that can be used as an assignment value assignment object.

Vector <int> v (10); // generate an int vector with a size of 10 and provide a template.


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


If operator [] returns pointer, the last statement must be written

* V [5] = 10;

In this way, the vector formed by v is not actually a pointer. Therefore, operator [] should always return a reference.


Therefore, conclusion: when you know that you need to point to something, but never change to something else, or when you implement an operator and its syntax requirements cannot be reached by pointer, you should select reference. In other cases, pointer is used.


Reprinted, Please retain the original article address: cnblogs.com/cgli/archive/2011/04/30/2033225.html

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.