First, the pointer:
(1) If the pointer is assigned to a dereference operation, the value of the object pointed to by the pointer is changed;
(2) If the pointer is not assigned to the dereference operation, then the value of the pointer itself is changed;
(3) The value of the const pointer itself cannot be modified, but the value of the object pointed to by the const pointer can be modified depending on whether the object it is pointing to is a const type.
For example:
Code Snippet 1:
String S1 ("some value"), string *sp1 = &s1;//at this time the value of S1 is "some value", the pointer SP1 points to s1string S2 ("another"); string *sp2 = &s2 ///At this time the value of S2 is "another", the pointer sp2 to S2*SP1 = "a new value";//The value of S1 at this time is "a new value", the pointer SP1 points to S1SP1 = sp2;//The pointer SP1 points to S2
Second, quote:
(1) A reference must be initialized when it is defined.
(2) Once the reference is initialized, it always points to a specific object, and if the reference is assigned, the value that references the associated object is modified.
Code Snippet 2:
int i1 = 1000,i2 = 2000;int *s1 = &i1, *s2 = &I2;S1 = s2; Result: S1 The value of the I1 pointed to is not changed, after the assignment operation ends, only changes the value of the pointer S1 itself, that is, the S1 pointer to another different object.
Code Snippet 3:
int &s1 = i1, &s2 = I2;S1 = S2; Result: The assignment action modifies the value S1 the I1 of the associated object, not the value of the reference itself. At the end of the assignment, reference S1 and reference S2 or point to the original object, except that the values of the two objects are equal.
Give a more complete code:
#include <iostream>using namespace Std;int main () {float S1 = 100,s2 = 200;cout<< "Original two values:" <<s1 << " "<<s2<<endl;float *sp1 = &s1, *sp2 = &s2;cout<<" Two values after first operation: "<<s1 <<" "<<s2 <<endl;cout<< "two pointer value SP1 =" <<*sp1<< "SP2 =" <<*sp2<<endl;cout<< "--------- --------------------"<<endl;//refers to the pointer, changing the value of the pointer object S1, from 100 to 10, corresponding to the above pointer theory (1) *sp1 = 10;cout<<" Two values after the second operation: value of "<<s1 <<" "<<s2<<endl;cout<<" two pointers SP1 = "<<*sp1<<" SP2 = "<& lt;*sp2<<endl;cout<< "-----------------------------" <<endl;//does not change the value of the object s1 the pointer, changing the value of the pointer itself, At this point the pointer SP1 points to SP2, corresponding to the above pointer Theory (2) SP1 = sp2;cout<< "Two values after the second operation:" <<s1 << "" <<s2<<endl;cout< < "two pointer value SP1 =" <<*sp1<< "SP2 =" <<*sp2<<endl;cout<< "-----------------------------" <<endl;//changes the value of the pointer object S2 at this time, and the value of the pointer sp2 changes *SP1 = 15;cout<< "Two values after the third operation:" <<S1 << "<<s2<<endl;cout<<" two pointer values SP1 = "<<*sp1<<" SP2 = "<<*sp2<<endl; cout<< "-----------------------------" <<endl;cout<< "" <<endl;float &i1 = s1, &i2 = s2; cout<< "Two values after fourth operation:" <<s1 << "<<s2<<endl;cout<<" two referenced values S1 = "<<s1<<" S2 = "<<s2<<endl;cout<<"-----------------------------"<<endl;//changes the reference assignment to the value of the associated object i1 = 30; cout<< "Two values after fourth operation:" <<s1 << "<<s2<<endl;cout<<" two referenced values S1 = "<<s1<<" S2 = "<<s2<<endl;cout<<"-----------------------------"<<endl;//at this point only changes the value of the S1 of the object associated with the I1 reference. Just refer to I1 and I2 or point to their respective objects, except that their values are equal. I1 = i2;cout<< "Two values after fifth operation:" <<s1 << "<<s2<<endl;cout<<" two reference value S1 = "<<s1 << "s2 =" <<s2<<endl;cout<< "-----------------------------" <<endl;return 0;}
Subsequent updates ...
C + + Learning basics Two--the difference between pointers and references