-5

Lets say I have

void swap(int &x, int &y){
  //do swap here
 }

Why is it legal to do: x = y inside of the function so that it assigns the VALUE of y to x?

Why isn't some sort of dereferencing needed?

  • 7
    perhaps you should take a good book on C++ (or a good web tutorial) and learn about references? – Walter May 25 '17 at 15:50
  • This should be covered near the beginning of your C++ textbook. –  May 25 '17 at 15:50
  • Possible duplicate of [What are the differences between a pointer variable and a reference variable in C++?](https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in) – Rosme May 25 '17 at 15:56
  • You can think of references as pointers that automatically dereference (that's essentially what's happening behind the scenes). – Barmar May 25 '17 at 16:08

1 Answers1

6

There's no dereferencing needed since references are not pointers. They are aliases to the referenced object.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70