-2

I would like to obtain some clarifications about References and Pointers :

  1. What are the points of similarities and Differences between References and Pointers? (Actually I am looking more about the "execution mechanism" kind of differences and not the theoretical ones)

  2. Is there any rule of thumb as to where either of them are to be used?

  3. Which of them is more useful compared to the other.

The reason for this question is that there is often confusion regarding the method of execution of statements containing references and pointers.

Thanks in advance.

iluvthee07
  • 491
  • 1
  • 5
  • 16
  • 2
    Your questions are too broad for useful answers. In particular, references play significantly different roles in C++ and Java (which you've both tagged here). – chrylis -cautiouslyoptimistic- Aug 20 '13 at 16:12
  • @Gabriel : I saw the link you provided. Actually I was looking more about the type of execution mechanism kind of differences and not the theoretical ones. – iluvthee07 Aug 20 '13 at 16:16
  • @chrylis : Thanks for pointing it out. I have removed the tag for Java. – iluvthee07 Aug 20 '13 at 16:18
  • Execution mechanisms can vary between compilers and between actual uses, and there are a lot of different-looking things that can easily compile to the same code. C++ implementations are required to produce the behavior specified by the program, not to preserve source distinctions. In this case, I'd be surprised to find any execution mechanism difference between references and pointers. – David Thornley Aug 20 '13 at 16:27

2 Answers2

1

In C++:

  1. references can't be reseated and must refer to an object, non-const pointers can be changed to point to something else and pointers may point to nothing at all (nullptr). They both act as a handle to some underlying object.
  2. Generally, pointers are used mostly with smart pointers to handle objects created on the heap and references are used mostly to pass objects around by reference.
  3. They are different tools and to say one is more useful than the other isn't helpful.
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
1
  1. A reference is a pointer which must be given an object to point to at initialization, cannot be changed to point to another object, and uses different syntax.

  2. References must be used in operator overloading, to give visually pleasing results. References are commonly preferred as function parameters, unless a NULL value is required. A special rule allows a temporary object to be passed to a const reference.

  3. Strictly compared to each other? Pointers.

"there is often confusion regarding the method of execution of statements containing references and pointers"

I have no idea what this means. If you convert code with references to the equivalent code with pointers, it should work the same.

References are pointers which look better and have some restrictions on use. That's all.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • I meant that I often get confused as to what is logically supposed to be used at a particular place in a code. Anyways point taken! Thanks – iluvthee07 Aug 20 '13 at 16:25
  • 2
    @iluvthee07 It is usually a question of personal choice, unless a restriction on references means pointers must be used. Don't agonize about deciding! – Neil Kirk Aug 20 '13 at 16:26