The difference between links and pointers

Possible duplicate:
What are the differences between a pointer variable and a reference variable in C ++?

What is class & mean in C ++ and how does it differ from class *?

Class& foo; Class* foo; 
+7
c ++ pointers reference
source share
7 answers

Version and represents the link, while * version represents the pointer. The difference is too big for a typical SO message. I suggest you start with the C ++ FAQ lite

http://www.parashift.com/c++-faq-lite/references.html

I usually don’t like to reply to the message “you must use Google”. However, this is one of the topics that I highly recommend Google to you. In particular, google "C ++ pointers against links". There is a lot of information on this subject, and the discussions on these pages will surpass everything that we write here.

+20
source share

* is a pointer, and this is a link. The difference between the two is that the pointer is a region of memory that needs to be dereferenced, for example. using the → operator to be “visible” as an instance of the class. The link instead is an alias, just an alternate name for the same instance of the class. You do not need to use the → operator with a link. You are using the dot operator.

Personally, I rarely used references, mainly when I had a value object that I allocated on the stack. The new operator always returns a pointer, which you must then dereference. Moreover, one of the most problematic link problems is that you cannot set them to NULL. In some cases, it is convenient to have a function that accepts either an object pointer or NULL. If your function accepts the link, you cannot pass NULL (you can use the Null object template, however)

+6
source share

Class * can point to any object of the class or not.

A class always points to one class object and can never point to another.

In addition, I believe that Bjarne is a member of a set of people who claimed that “arrays in C are broken without repair”, the class * can point to a whole array of ding-dang objects of class objects, built one after the other in memory, and in C There is absolutely no way to determine if a class * points to one or more.

+5
source share

Another difference is that reference variables must be initialized. You cannot create a reference variable, as shown in the code example. This will result in a compiler error.

0
source share

A reference (&) is the same as a pointer (*), except that the C ++ compiler ensures that it is not NULL. However, it can still be a dangling pointer (a pointer variable that has no reference, so that it is garbage and is invalid for any use).

0
source share

As indicated, you should use it, but to avoid confusion:

  • Links are not variables
  • Links are not like pointers (but you can use them in a similar way)

Think of the link as a shortcut to the term assigned to it.

0
source share

Another tip I suggest is this:

Use links when you can, pointers when you need to. If the object is guaranteed to exist, you should probably use a link. If not, you may have to use a pointer.

Another advantage is that links eliminate ambiguity regarding ownership. As soon as the programmer sees the pointer, they will begin to wonder if they should delete it.

Check this example:

 // Wrapper class using a reference because the wrapped object always exists
 class wrapper
 {
 public:
   // If the wrapped is guaranteed to exist at creation, do it this way
   Wrapper (Wrapped & wrapped): _ wrapped (wrapped) {/ * empty * /}

   // put extra methods here.
   int getWrappedValue () const {return _wrapped.getValue ();  }

 private:
   Wrapped & _wrapped;  // This object always exists and is valid
 };

 // Wrapper class written to support a possibly non-existent wrapped object.
 class wrapper
 {
 public:
   Wrapper (Wrapped * wrapped = 0): _ wrapped (wrapped) {/ * empty * /

   void setWrappee (WRappee * wrapped) {_wrapped = wrapped;  }

   int getWrappedValue () const;  // Not making inline - more complex

 private:
   Wrapped * _wrapped;  // Always check pointer before use
 };

 int Wrapper :: getWrappedValue () const
 {
   if (_wrapped)
   {
     return _wrapped-> getValue ();
   }
   else
   {
     return -1;  // NOTE, this is a contrived example - not getting into exceptions
   }
 }
0
source share

All Articles