Home c++ What is the difference between links from pointers in C++

What is the difference between links from pointers in C++

Author

Date

Category

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 :

  1. You can not declare an array of links.
  2. Links have no address.
  3. There is arithmetic pointers, but no arithmetic links.
  4. 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.

  5. 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.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions