How can C ++ programmers explain to a java engineer what is reference?

Source: Internet
Author: User

How can C ++ programmers explain to a java engineer what is reference?

After an Android programmer asked me about the role of extern "c" yesterday, today she asked me what is reference?

I replied to him, and the reference is an alias. I quoted you, and I changed, and you also changed.

Write another blog!

People who have never studied C ++ may have more questions: Why do they need to reference pointers? What are the differences between the two?

A pointer can be re-assigned any number of times while a reference can not be re-seated after binding.
The pointer can be re-assigned, but cannot be referenced.

int x = 5;int y = 6;int *p;p =  &x;p = &y;*p = 10;assert(x == 5);assert(y == 10);int x = 5;int y = 6;int &r = x;

Pointers can point nowhere (NULL), whereas reference always refer to an object.
The pointer can point to null, and the reference cannot be null.

You can't take the address of a reference like you can with pointers.
You cannot obtain a reference address.

A pointer has its own memory address and size on the stack (4 bytes on x86), whereas a reference shares the same memory address (with the original variable) but also takes up some space on the stack. since a reference has the same address as the original variable itself, it is safe to think of a reference as another name for the same variable. note: What a pointer points to can be on the stack or heap. ditto a reference. my claim in this statement is not that a pointer must point to the stack. A pointer is just a variable that holds a memory address. this variable is on the stack. since a reference has its own space on the stack, and since the address is the same as the variable it references. more on stack vs heap. this implies that there is a real address of a reference that the compiler will not tell you.

int x = 0;int &r = x;int *p = &x;int *p2 = &r;assert(p == p2);

You can have pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection.
A pointer can be pointed to a pointer and cannot be referenced or referenced. (Tongue twisters, but I believe you can understand)

int x = 0;int y = 0;int *p = &x;int *q = &y;int **pp = &p;pp = &q;//*pp = q**pp = 4;assert(y == 4);assert(x == 0);

Const references can be bound to temporaries. Pointers cannot (not without some indirection ):

const int &x = int(12); //legal C++int *y = &int(12); //illegal to dereference a temporary

Use references in function parameters and return types to define useful and self-documenting interfaces.
It is best to use references for function parameters. If the returned value is meaningful, it is best to return references. But do not return references to local variables.

Use pointers to implement algorithms and data structures
It is best to use pointers in algorithms and data structures.

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.