-2

I readed a lot about pointers and references, and they still confuse me, but i think it's just a matter of programming more to get better understanding since i just started a week ago with c++. One thing i keep wondering is how does a reference know where to point to? Or is it a reference to a pointer? Since a reference also has to point somewhere in memory right?

Also i saw on stackoverflow that in general use references when you can. But if i look at the source code of hl2 a lot of times pointers are used. And final, how long did it take for you guys to get a good feeling of when using pointers and when not?

clankill3r
  • 9,146
  • 20
  • 70
  • 126
  • possible duplicate of [What are the differences between pointer variable and reference variable in C++?](http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c) – Attila Oct 30 '13 at 16:13
  • 1
    "Also i saw on stackoverflow that in general use references when you can. But if i look at the source code of hl2 a lot of times pointers are used." So what? – Daniel Daranas Oct 30 '13 at 16:19

4 Answers4

5

You can think of a reference as a pointer that you can't change or get the value of. As to the difference between them and pointers, the biggest difference is that you can do pointer math on pointers, references only ever point to a single object and never change.

For a bit of intuition, if you're passing a large object to a function or method and don't want to copy it (let's not consider move constructors for now), you can just pass a const reference to it. The syntax inside the method will be just like you passed an object without copying even though the name that you're using will still refer to the original object. One added benefit you get with references is that they can never be null -- they'll always refer to something, even though that something might be reclaimed memory.

If however, you had a block of memory with a large number of objects stored one after the other, you can use a pointer to the beginning of that memory, use it, and then increment it by 1 to get to the next object.

yan
  • 20,644
  • 3
  • 38
  • 48
2

A reference is a pointer in disguise, and it has its target initialized at the very beginning of its lifetime. In other words:

int a = 2;
int& ra = a;

ra = ra + 2;

is equivalent to

int a = 2;
int* pa = &a;

*pa = *pa + 2;

but the reference version is syntactically easier, and is fundamentally unable to have something refer to null - there is no null reference, and there is no way to make ra 'point' to something else.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • 1
    Note that the code is also (approximately, ignoring scope issues) equivalent to `int a = 2; #define ra a ...`. Because references are primarily aliases, and "pointers in disguise" as a common implementation technique. – Steve Jessop Oct 30 '13 at 16:16
1

In principle this is similar to asking how a pointer knows where to point to, or how an integer knows how big to be, or how a variable name knows which variable it is.

The person who writes the compiler chooses how to make these things behave the way that the standard says they behave. Generally speaking, somewhere deep in the details a reference either "is" a variable name or "is" a pointer, depending on how it was initialized.

So, if you write:

int a = 1;
int &b = a;

Then the compiler might in effect note in its symbol table that in this scope, b and a are two names for the same object.

If you write:

void foo(int &a) { a = 1; }

Then the calling convention used by your compiler might dictate that this function's parameters are passed exactly the same as a function that takes a pointer. What the function does with the parameter once it has it is slightly different.

In the latter case that doesn't mean the reference "is" a pointer as far as the programmer is concerned, of course. Just that the compiler-writer uses a pointer somewhere, to represent the referand.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
0

A reference is just an alias for a variable or an object. It 'refers' to the variable/object to whom it is made for reference.

int a = 5;
int &x = a;

x and a refer to the same variable.

Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
  • It's not just an alias for a variable, a reference can refer to objects that are not variables. For example (although I don't recommend writing exactly this code), `int &x = *(new int());` – Steve Jessop Oct 30 '13 at 16:19