Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C++ beginners can't understand the pointer symbols * and&; How to use it? & Isn't it a quote? I don't understand it in the program, please explain it in detail, thank you!
C++ beginners can't understand the pointer symbols * and&; How to use it? & Isn't it a quote? I don't understand it in the program, please explain it in detail, thank you!
1) reference must be initialized, and pointer is not necessary.

2) The reference cannot be changed after initialization, and the pointer can change the object it points to.

2) There is no reference to the null value, but there is a pointer to the null value.

C++ pointers and references look completely different (pointers use operators' *' and'->; , reference using operator''. ), but their functions seem to be the same. Both C++ pointers and references allow you to indirectly refer to other objects. How to decide when to use pointers and when to use references?

First of all, realize that under no circumstances can you use null references. References must always point to an object. So if you use a variable and point it to an object, but the variable may not point to any object at some time, you should declare the variable as a pointer, because then you can assign a null value to the variable. Conversely, if the variable explicitly points 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 a moment," you asked doubtfully. "What will happen to such code?"

char * PC = 0; //Set the pointer to null char &;; Rc = * pc// It's harmful to point references to null values, no doubt. The result will be uncertain (the compiler can produce some output, so anything can happen), and people who write such code should be avoided unless they agree to correct the mistakes. If you are worried that such code will appear in your software, then you'd better avoid using references completely or let a better programmer do it. We will ignore the possibility that references will point to null values in the future.

Because references must point to an object, in C, references should be initialized.

Chord and chord. RS; //Error, the reference must be initialized to the string s ("xyzzy"); Chord and chord. RS = s; //correct, rs points to S.

Pointers are not so limited.

String * ps// Uninitialized pointer//Legal but dangerous

The fact that there are no references to null values means that code that uses references is more efficient than code that uses pointers. Because you don't need to test the validity of a reference before using it.

Void printDouble (constant double & amp rd) {cout <; & ltrd; //There is no need to test rd, it}//definitely points to a double value.

Instead, you should always test pointers to prevent them from being empty:

Void print double (const double * PD) {if (PD) {//Check whether it is null cout.

Another important difference between C++ pointers and references is that pointers can be reassigned to point to different objects. However, the reference always points to the object specified at initialization and cannot be changed later.

The string s1("Nancy"); The string S2 ("Clancy"); Chord and chord. RS = s 1; //RS reference s 1 string * PS = &; s 1; // ps points to s1RS = S2; // rs still refers to s 1,//but the value of s 1 is now///" Clancy "PS = &;; S2; // ps now points to S2; // s 1 has not changed.

Generally speaking, you should use the pointer 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 be empty); Secondly, you need to be able to point to different objects at different times (in this case, you can change the direction of the pointer). If you always point to an object, once you point to an object, you won't change this point, then you should use a reference.

In another case, when an operator is overloaded, a reference should be used. The most common example is the operator []. The typical use of this operator is to return a target object that can be assigned a value.

Vector v (10); //Establish a shaping vector with the size of 10; //Vector is the template V [5] =10 in the standard C library; //The target object of assignment is the value returned by the operator []. If the operator [] returns a pointer, the following statement must be written as follows:

* v[5]= 10; But this will make v look like a vector pointer. So you will choose to let the operator return a reference.

You should not use pointers when you know that you must point to an object and don't want to change its pointing, or when overloading operators to prevent unnecessary semantic misunderstanding. In other cases, pointers should be used.