Differences between c ++ pointers and references

Source: Internet
Author: User

Pointers and references look completely different (pointers use the operators "*" and "->" and reference the operators "."), but they seem to have the same functionality. Both pointer and Reference allow you to indirectly reference other objects. How do you decide when to use pointers and when to use references?

First, you must realize that reference pointing to null values cannot be used under any circumstances. A reference must always point to some objects. Therefore, if you use a variable and direct it to an object, but the variable may not point to any object at some time, you should declare the variable as a pointer, in this way, you can assign a null value to the variable. On the contrary, if the variable must point to an object, for example, your design does not allow the variable to be empty, then you can declare the variable as a reference.

"But please wait." You asked with suspicion, "What are the consequences of such code ?"Copy codeCode: char * pc = 0; // set the pointer to a null value
Char & rc = * pc; // point the reference to a null value

This is very harmful and there is no doubt. The result will be uncertain (the compiler can generate some output, which may lead to any issue ). Avoid writing such code unless they agree to correct the error. If you are worried that such code will appear in your software, you 'd better avoid using references completely, or else let better programmers do it. In the future, we will ignore the possibility that a reference points to a null value.
Because the reference will certainly point to an object, in C ++, the reference should be initialized.Copy codeThe Code is as follows: string & rs; // error. The reference must be initialized.
String s ("xyzzy ");
String & rs = s; // correct. rs points to s.

Pointers do not have such restrictions.Copy codeCode: string * ps; // uninitialized pointer
// Valid but dangerous

The fact that there is no reference pointing to a null value means that the code to be referenced is more efficient than the pointer. Because you do not need to test its validity before using the reference.Copy codeCode: void printDouble (const double & rd)
{
Cout <rd; // No need to test rd. It must point to a double value.
}

Instead, pointers should always be tested to prevent being empty:Copy codeCode: void printDouble (const double * pd)
{
If (pd) // check whether it is NULL
{
Cout <* pd;
}
}

Another important difference between a pointer and a reference is that a pointer can be re-assigned to point to another object. However, the reference always points to the specified object during initialization and cannot be changed later.Copy codeThe Code is as follows: string s1 ("Nancy ");
String s2 ("Clancy ");
String & rs = s1; // rs references s1
String * ps = & s1; // ps points to s1
Rs = s2; // rs still references s1, but the value of s1 is "Clancy"
Ps = & s2; // ps is now directed to s2;
// S1 has not changed

In general, you should use pointers in the following situations. First, you should consider the possibility of not pointing to any object (in this case, you can set the pointer to null ), second, you need to be able to point to different objects at different times (in this case, you can change the pointer ). If you always point to an object and it does not change its direction once it points to an object, you should use references.

Another case is that when you reload an operator, you should use references. The most common example is the operator []. A typical use of this operator is to return a target object, which can be assigned a value.Copy codeCode: vector <int> v (10); // create an integer vector (vector) with a size of 10;
V [5] = 10; // the target object to be assigned is the value returned by the operator [].

If the operator [] returns a pointer, the next statement must be written as follows:Copy codeThe Code is as follows: * v [5] = 10;

However, this will make v look like a vector pointer. Therefore, you will choose to let the operator return a reference.

When you know that you must point to an object and do not want to change its direction, or when you overload operators and prevent unnecessary semantic misunderstanding, you should not use pointers. In other cases, pointer should be used.

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.