What is the difference between the C ++ pointer and the reference?

Source: Internet
Author: User

The C ++ programming language is very powerful. It not only supports the functions used in the C language, but also has the special functions of the C ++ language. Here we will introduce in detail the differences between references and pointers in C ++, let everyone have a clear understanding of the C ++ pointer and reference functions.

  • Analysis of basic application methods of C ++ youyuan Functions
  • Usage of C ++ Constructor
  • How to parse the C ++ operation clipboard
  • Detailed Implementation of C ++ string replacement Functions
  • Introduction to multiple implementation techniques of the C ++ Singleton Mode

C ++ pointers and references look completely different. Pointers use the '*' and '->' operators and references use the '.'), but they seem to have the same functionality. Both the C ++ 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 the reference pointing to a null value 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 ?"

 
 
  1. Char * pc = 0; // set the pointer to a null value
  2. Char & rc = * pc; // point the reference to a null value

This is very harmful and there is no doubt. The result would be that an uncertain compiler could generate some output, which could lead to any possible occurrence). The compiler should 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.

 
 
  1. String & rs; // error. The reference must be initialized.
  2. String s ("xyzzy ");
  3. String & rs = s; // correct. rs points to s.

Pointers do not have such restrictions.

 
 
  1. String * ps; // uninitialized pointer
  2. // 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.

 
 
  1. Void printDouble (const double & rd)
  2. {
  3. Cout <rd; // No need to test rd, it
  4. } // Definitely points to a double value

Instead, pointers should always be tested to prevent being empty:

 
 
  1. Void printDouble (const double * pd)
  2. {
  3. If (pd) {// check whether it is NULL
  4. Cout <* pd;
  5. }
  6. }

Another important difference between the C ++ pointer and the reference is that the pointer can be re-assigned to point to another different object. However, the reference always points to the specified object during initialization and cannot be changed later.

 
 
  1. String s1 ("Nancy ");
  2. String s2 ("Clancy ");
  3. String & rs = s1; // rs references s1
  4. String * ps = & s1; // ps points to s1
  5. Rs = s2; // rs still references s1,
  6. // But the value of s1 is
  7. // "Clancy"
  8. Ps = & s2; // ps is now directed to s2;
  9. // S1 has not changed

In general, you should use pointers in the following situations. First, you may consider the possibility that the pointer does not point 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.

 
 
  1. Vector v (10); // create an integer vector with a size of 10;
  2. // Vector is a template in the Standard C library
  3. 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:

 
 
  1. *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. The above is an introduction to the differences between the C ++ pointer and the reference.

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.