Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams Where did you hear this? And to answer your question: you dereference pointers. A reference is not a pointer, it's a reference. GManNickG Jul 10, 2011 at 3:54 It was briefly mentioned in my course notes and I am not sure about its meaning. Also, Wiki seems too vague. If the term doesn't exist feel free to close the question. Mark Jul 10, 2011 at 3:57

I assume that your teacher was trying to explain the difference between pointers and references.

It is relatively common (though not technically accurate) to refer to references as fancy pointers that do implicit de-referencing.

int   x    = 5;
int*  xP   = &x;
int&  xR   = x;
xR  = 6; // If you think of a reference as a fancy pointer
         // then here there is an implicit de-reference of the pointer to get a value.
*xP = 7; // Pointers need an explicit de-reference.

The correct way to think about is not to use the "A reference is a fancy pointer". You need to think about references in their own terms. They are basically another name for an existing variable (AKA an alias).

So when you pass a variable by reference to a function. This means the function is using the variable you passed via its alias. The function has another name for an existing variable. When the function modifies the variable it modifies the original because the reference is the original variable (just another name for it).

So to answer you question:

I don't need the & in front of it to use its value?

No you don't need to add the &.

int f(int& x)  // pass a value by reference
    x =5;
int plop = 8;
f(plop);
// plop is now 5.

Another context in which C++ will implicitly dereference pointers is with function pointers:

void foo() { printf("foo\n"); }
void bar() {
  void (*pf)() = &foo;
  (*pf)(); // Explicit dereference.
  pf(); // Implicit dereference.
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.