NOGDUS $1670.00 has been donated to NOGDUS!
May 28, 2017, 09:01:59 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
   Home   Help Search Login Register  
Pages: [1]   Go Down
Author Topic: About References in C++  (Read 1494 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Offline Offline

Respect: 3425
« on: October 31, 2008, 09:42:47 AM »

You have probably heard about references in C++ code.
I am here to shed a little light on the subject.
I did not write this; it is a snippet from a writing of Michael J. Dickheiser.

begin snippet
There are several major, very important differences between references and pointers:

  • When working with a reference, we use the same syntax as when working with an object. Instead of using the operator -> to dereference the pointer and access member functions and variables, a reference uses a dot (.) operator, just like a regular object.
  • References can be initialized only once. A pointer can point to a certain object and then, at any time, be changed to point to a different object. References are different. After they have been initialized to refer to an object, they cannot be changed. In that sense, they behave like const pointers.
  • References cannot be NULL. This is a consequence of the first two points. Because references must be initialized right away with a real object, and because they cannot be changed, they can never be NULL, unlike a pointer. Unfortunately, this does not mean that what they refer to is valid. It is possible to delete the object a reference is referring to, or to trick a reference through casting to point to NULL.
  • References cannot be deleted nor created like a pointer. In that sense, they are the same as an object.

When to use References:

Are pointers obsolete then? Should we use references all the time? Not at all. A good rule of thumb is to use references whenever possible. They are the cleaner, less error-prone interface. However, some situations still require pointers.

If an object must be created or deleted dynamically, we have to use a pointer to that object. Usually, the owner of a dynamically created object keeps a pointer to it. If anybody else has to use that object, they can use a reference to make it clear that they are not responsible for freeing that object. If at some point the ownership of the object must be transferred, it should be done through a pointer instead of a reference.

Sometimes we have to change the object we are pointing to. In that case, unless we change the structure of the program, a pointer is the only way to go because a reference can never point to a different object.
At other times we actually rely on the fact that a pointer can be NULL, either by returning a NULL pointer from a function as a sign that the function failed, or as an optional parameter to a function. References cannot refer to NULL, so they do not serve that purpose. It is questionable whether or not a good program design relies on pointers sometimes being NULL. A better solution might be to refactor the program to indicate failed functions in a different way, and to use references instead.
The last reason for using a pointer over a reference is pointer arithmetic. With pointer arithmetic, we can iterate through a section of memory, interpreting its contents based on the type of pointer we are using.

end snippet

I personally dislike using references, and I stick with pointers, but that is because I started with C, and I am very comfortable with using pointers. You are free to use whatever you like, but I suggest sticking with pointers if you already make use of them in your code. I just thought that this information would help you to read other C++ code that you may come across in your life as a programmer. Cool

Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!