Differences and associations between references and Pointers in c++

  • 2020-04-02 02:19:02
  • OfStack

References and Pointers in C++

u Similarities: 1. The concept of address;
Pointer to a block of memory whose contents are the address of the memory; A reference is an alias for a block of memory (a reference in Java actually means an alias).

u The difference between: 1. A pointer is an entity, and a reference is an alias;
2. References need not be dereferenced (*) when used, and Pointers need to be dereferenced;
3. A reference can only be initialized once at definition time and cannot be changed later; Pointer variable; Quote "from one to the last"
4. Reference without const, pointer with const, const pointer immutable;
5. References cannot be null, Pointers can be null;
6. "sizeof reference" gets the sizeof the referenced variable (object), while "sizeof pointer" gets the sizeof the pointer itself (the address of the referenced variable or object). Typeid (T) = = typeid (T & Sizeof (T) == sizeof(T) & ) is true, but when a reference is a member, it takes up the same space as a pointer (no standard specification was found).
7. Pointer and reference of the self-increment (++) operation meaning is not the same;

u contact
1. References are implemented with Pointers within the language (how?) .
2. For general applications, there is no serious semantic error in understanding a reference as a pointer. A reference is a pointer that is operationally restricted (only content fetching is allowed).
References are a concept in C++, and beginners tend to confuse references with Pointers. In the following program, n is a reference of m, and m is the referent.


int m;
int &n = m;

N is the alias (nickname) of m, and any operation on n is an operation on m. For example, there is wang xiaomao, whose nickname is "three hairs". Say "three hairs" how how, in fact is to wang xiaomao say three words four. So n is neither a copy of m, nor a pointer to m, but n is m by itself.

Some of the rules cited are as follows:
(1) the reference must be initialized when it is created (the pointer can be initialized at any time).
(2) there cannot be a NULL reference, which must be associated with a valid storage location (the pointer can be NULL).
(3) once the reference is initialized, the relationship of the reference cannot be changed (the pointer can change the referred object at any time).
In the following sample program, k is initialized as a reference to I. The statement k = j does not modify k to be a reference to j, it simply changes the value of k to 6. Since k is a reference to I, the value of I also becomes 6.


int i = 5;
int j = 6;
int &k = i;
k = j; // k  and i  All of the values of 6;

The above program looks like a word game and does not show the value of the reference. The main function of the reference is to pass the arguments and return values of the function. In C++, there are three ways to pass the arguments and return values of functions: value, pointer, and reference.

The following is a sample program for value passing. Since the x in Func1 function is a copy of the external variable n, changing the value of x does not affect n, so the value of n is still 0.


void Func1(int x)
{
x = x + 10;
}
int n = 0;
Func1(n);
cout <<  " n =  "  << n << endl;// n = 0

The following is a sample program for pointer passing. Since the x in Func2 function is the pointer pointing to the external variable n, changing the contents of the pointer will cause the value of n to change, so the value of n becomes 10.


void Func2(int *x)
{
(* x) = (* x) + 10;
}
⋯
int n = 0;
Func2(&n);
cout <<  " n =  "  << n << endl; // n = 10

The following is a sample "pass by reference" program. Since the x in Func3 is a reference to the external variable n, x and n are the same thing, changing x is equal to changing n, so the value of n becomes 10.


void Func3(int &x)
{
x = x + 10;
}
⋯
int n = 0;
Func3(n);
cout <<  " n =  "  << n << endl; // n = 10

Comparing the three sample programs above, we can find that "pass by reference" is like "pass by pointer" in nature, and "pass by value" in writing. In fact, "reference" can do anything "pointer" can do, why even "reference" this thing? The answer is "do the right job with the right tools." Pointers can manipulate things in memory without constraints, and while Pointers are powerful, they are dangerous. Like a knife, it can be used to cut trees, cut paper, manicure, haircut, etc., who dare to use it? If you really only need to borrow the "alias" of an object, use "reference" instead of "pointer" to avoid accidents. For example, someone needs a proof that the seal of the official seal should have been stamped on the document. If the key to the official seal is given to him, then he has gained the right that he should not have.

Note: if string s1(" ABC ") is defined; String * p = & S1. So the p value is the address of s1, so cout < < P1 output is equal to cout < < & S1,; P value is the content stored in the address indicated by pointer p, so cout < < P is equal to ABC; & P is the address of the pointer p itself, and the value stored in the address is the address of the referred content, cout < < & P is equal to the memory address of the pointer p itself

Type in the following code to verify :(and verify that "references are immutable, Pointers are mutable")


#include<string>
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
string s1("Nancy");
string s2("Clancy");
string &rs=s1;
string *ps=&s1;
cout<<&rs<<" "<<ps<<"n";
rs=s2;
ps=&s2;
cout<<rs<<" "<<*ps<<"n";
cout<<&rs<<" "<<&s2<<" "<<ps<<" "<<&ps;
//The address of the reference rs is the same as before, which is still equal to the address of s1, which has not changed,
//The address of the pointer ps has changed and points to s2, referring to the values and within rs
//The values stored in the address indicated by ps are changed to s2
_getch();

}


Related articles: