Differences and links between references and pointers in C + + _c language

Source: Internet
Author: User

References and pointers in C + +

Same point: 1. Are the concept of the address;
The pointer points to a piece of memory whose contents are the address of the memory, and the reference is an alias to a block of memory (the reference in Java is actually the meaning of the alias).

Difference: 1. The pointer is an entity and the reference is only an individual name;
2. References need not be referenced (*), the pointer needs to dereference;
3. References can only be initialized once at the time of definition, and then immutable; pointer variable; reference "one-woman"
4. The reference does not have a const, the pointer has a const,const pointer immutable;
5. The reference cannot be null, the pointer can be empty;
6. The "sizeof reference" gets the size of the variable (object) to which it is pointed, and the "sizeof pointer" gets the size of the pointer itself (the address of the variable or object being pointed to); typeID (t) = = typeid (t&) constant is true, sizeof (t) = = sizeof (t&) is true, but when referenced as a member, it occupies the same space as the pointer (no standard rules are found).
7. The pointer and the reference of the self-increase (+ +) operation is not the same meaning;

Contact
1. References are implemented using pointers within the language (how do I implement them?) )。
2. For general applications, a reference to a pointer does not make a serious semantic error. A reference is a pointer to an operation that is restricted (only the fetch operation is allowed).
References are concepts in C + +, and beginners tend to confuse references with pointers. In the program, N is a reference to M (reference), and M is a referenced object (referent).

Copy Code code as follows:

int m;
int &n = m;

N is equivalent to M's alias (nickname), and any action on N is an operation on M. For example, someone named Wang Xiaomao, his nickname is "Sanmao". Say "Sanmao" how how, in fact, is to Wang Mao. So n is neither a copy of M nor a pointer to M, in fact N is the M itself.

Some of the rules cited are as follows:
(1) When a reference is created, it must be initialized (the pointer can be initialized at any time).
(2) cannot have a null reference, the reference must be associated with a legitimate storage unit (the pointer can be null).
(3) Once the reference is initialized, the referenced relationship cannot be changed (the pointer can change the object at any time).
In the following example program, K is initialized to a reference to I. The statement k = J does not change K to be a reference to J, but changes the value of K to 6. Since k is a reference to I, the value of I also becomes 6.

Copy Code code as follows:

int i = 5;
int j = 6;
int &k = i;
K = J; The values of k and I all turn to 6;

The above program looks like it's playing word games and doesn't reflect the value of references. The primary function of a reference is to pass the function's arguments and return values. In the C + + language, the parameters and return values of functions are passed in three different ways: value passing, pointer passing, and reference passing.

The following is a sample program for value delivery. Since x in the FUNC1 function body 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.

Copy Code code as follows:

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 delivery." Since x in the FUNC2 function body is a pointer to an external variable n, changing the contents of the pointer will cause the value of N to change, so the value of n becomes 10.

Copy Code code as follows:

void Func2 (int *x)
{
(* x) = (* x) + 10;
}

int n = 0;
FUNC2 (&n);
cout << "n =" << n << Endl; n = 10

The following is a sample program for reference delivery. Since the x in the body of the FUNC3 function is a reference to the external variable n, x and n are the same thing, changing x equals changing n, so the value of n becomes 10.

Copy Code code as follows:

void Func3 (int &x)
{
x = x + 10;
}

int n = 0;
FUNC3 (n);
cout << "n =" << n << Endl; n = 10

Comparing the three sample programs above, you will find that the nature of the reference pass is like "pointer passing", and the writing is like "value delivery." In fact, "pointers" can also do anything that "references" could do, why "quote" this thing? The answer is "to do the right thing with proper tools". Pointers can manipulate what is in memory without constraint, although the pointer is powerful but dangerous. Like a knife, it can be used to cut trees, paper, manicure, Barber and so on, who dares to use this? If you really just need to borrow an object's "alias", use "reference" instead of "pointers" to avoid surprises. For example, a person needs a certificate, originally in the document stamped seal on the line, if the official seal of the key to him, then he won the wrong right.

Note: If you define string S1 ("abc"); String * p=&s1; the address of the S1 (that is, the content within the pointer p), the COUT<<P1 output value is equal to COUT<<&S1, P value is the content stored in the address of the pointer p, so cout<<p equals abc;&p is the address of the pointer p itself, and the value in the address is the address of the content, Cout<<&p equals the address of the memory of the pointer P itself.

can be typed as follows code validation: (and can verify "reference immutable, pointer variable")

Copy Code code as follows:

#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, or is equal to S1 's address, unchanged.
The address of the pointer PS changes and points to the S2, referencing the values in RS and
The value in the address that the pointer PS refers to is changed to S2

_getch ();


}

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.