The difference between references in C + + and pointers

Source: Internet
Author: User
Tags constant

★ The same point:

1. The concept of 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.

★ 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, and the pointer is variable;

Refer to the "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" obtains 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 to which it is directed);

typeID (t) = = typeid (t&) constant is true, sizeof (t) = = sizeof (t&) constant is true, but when referenced as a member, it occupies the same space as the pointer (no Standard rules 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).

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 has also become 6.

int i = 5;

int j = 6;

int &k = i;

The values of k = j;/k and I are all changed 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.

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

Related Article

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.