4

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

I got my head around pointers in C (the basics anyway) and started reading up on C++. The book I'm reading jumps straight into references, and looking in the index doesnt got on to pointers until later on.

In C, I thought if I wanted to do a pass by reference function I would have to use pointers as arguments, e.g.

void swapAandB(int *A, int *B){

//do something
}

But the C++ book, decides to put references to the original variable into the function. e.g.

void swapAandB(int& A,  int& B){

 //do something
}

My C++ book hasn't explained why we don't use pointers as in C. So I'm a little confused. I guess my question is what's going on here?

Community
  • 1
  • 1
  • 1
    You *could* use pointers in c++ but it's just harder to keep track of – qwertymk Nov 14 '11 at 14:36
  • just deleted my answer here and instead added to my answer in the duplicate: http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c/596750#596750 – Christoph Nov 14 '11 at 15:31

4 Answers4

5

References are and additional mechanism that C++ provides compared to C. Using pointers in C++ is perfectly legal, so you could still define your first function unmodified:

void swapAandB(int *A, int *B){

 //do something
}

The main advantage that references offer over pointers is that it is not that easy to have the equivalent of a NULL pointer. But, references, both semantically and syntactically go well beyond this in shaping C++ features as a language. I think this will become clearer once you get more into the language. Anyway, you can try and read this paper about the difference between pointers and references.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • Thanks, I think that paper will clear things up after considering it. Problem was, I stormed into the C++ book thinking it would just be "C with classes" and assumed referencing variables would be just like C. –  Nov 14 '11 at 14:44
  • 1
    @JJG: C++ is not just C with classes. You'll need to forget everything you know about C to get your head around C++. – Gabriel Schreiber Nov 14 '11 at 15:10
2

You can use pointers as in C of course but references are objects aliases, so, the object exist, you don't need to check if their are valid. There are differences with pointers in their use:

void foo(Type * pointer)
{
    if (pointer)
        pointer->data_ = ....;
}

void foo(Type & reference)
{
    reference.data_ = ....;
}

Type obj;

foo(&obj);    // pointer syntax
foo(obj);     // reference syntax

Besides, a reference always 'point' to the same object, so you will be sure always of using it correctly.

Tio Pepe
  • 3,071
  • 1
  • 17
  • 22
0

In this case pointers and references will behave the same way.
The only difference is how you call them:

int x, y;
swapAandB( &x, &y ); // here you pass the variables' addresses to the pointer function
swapAandB( x, y ); // here you pass the variables' reference to the reference function

The result is the same, the variables don't get copied, but rather referenced from within the functions, and any changes you apply to them inside the function will affect the variables in the calling scope.

Petruza
  • 11,744
  • 25
  • 84
  • 136
-1

As a rule: Try to use references instead of pointers.

In some cases you need to use pointers:

  • polymorphism
  • you need to have something like a null pointer
  • you need to delete a variable later on
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
tgmath
  • 12,813
  • 2
  • 16
  • 24