What is the fundamental difference between the reference from the pointer in C++? When it is better to use the link, and when the pointer? What restrictions are the first, and what are the second?
Answer 1, Authority 100%
More differences :
- You can not declare an array of links.
- Links have no address.
- There is arithmetic pointers, but no arithmetic links.
-
The pointer may have a “non-valid” value with which it can be compared before use.
If the calling side cannot but transfer the link, the pointer may have a special
NULLPTR
:void f (int * num, int & amp; num2) { if (num! = nullptr) // if nullptr ignored algorithm { } // CAN'T CHECK NUM2 ON NEED TO USE OR NOT }
http://rextester.com/eqmc52074
(Standart )
A NULL POINTER CONSTANT IS AN INTEGER LITERAL (2.13.2) WITH VALUE ZERO OR A PRVALUE OF TYPE STD :: NULLPTR_T. A NULL POINTER CONSTANT CAN BE CONVERT TO A POINTER TYPE; The Result Is The Null Pointer Value of That Type and Is Distinguishable From Every Other Value of Object Pointer or Function Pointer Type. -
link does not have a qualifier
const
# include & lt; iostream & gt; INT MAIN () { STD :: COUT & LT; & LT; "Hello, WORLD! \ N"; const int v = 10; // INT & AMP; const R = V; // Error Const int & amp; r = v; enum { is_const = std :: is_const & lt; decltype (r) & gt; :: value }; if (! is_const) STD :: COUT & LT; & LT; "Const int & amp; r is not const \ n"; ELSE. STD :: COUT & LT; & LT; "Const int & amp; r is const \ n"; }
About Welcome
Some refer to an excerpt with an interview with Sturastrup:
Obviously implementing the reference is (constant) pointer, each time the use of which is retrieved. In some cases, the compiler can optimize the reference in such a way that during execution there will be no object representing the link.
Others set only one question in response:
What is the ratio of the direction of the pointer?
on the topic, do you need to know the differences of the pointer from the link, wrote Joel Spolly in his article “Abstractions . “
Answer 2, Authority 86%
Principal differences Two:
- reference, uninitialized, uninitialized;
- link cannot be changed after initialization.
From here and we get the pros and cons of the use of both:
- links are better to use when it is undesirable or not planned to change the link link → object;
- pointer is better to use when the following moments are possible during the Links Links:
- link does not indicate any object;
- link indicates different objects during its lifetime.