1

My understanding is that a reference is basically a pointer with a value that doesn't change, and is always dereferenced. So if I have the code

int& thisIsAReference = someVariable;

Then basically a pointer is created to the location of someVariable, and that pointer can never point to another location and is always dereferenced.

But that seems to be the same thing as a variable. As I understand it a variable refers to a particular memory location, can't refer to a different memory location, and implicitly refers to the value at a location rather than the location itself.

So, other than the differing syntax of declaration, is there any difference between thisIsAReference and someVariable?

Raiden Worley
  • 394
  • 1
  • 4
  • 14
  • Are you asking what's the difference between `int x = y` and `int& x = y`? – SergeyA Mar 10 '16 at 19:28
  • 1
    No. I'm aware that the first would just copy the value. I'm asking if after the second there's any difference between `x` and `y`. Not in value but in how they're treated by the compiler and such. – Raiden Worley Mar 10 '16 at 19:30
  • @RaidenWorley You can use the reference like an alias. It will resolve to the same value as `someVariable` has at any time. – πάντα ῥεῖ Mar 10 '16 at 19:33
  • References *are* variables. They're not *objects*, though. – Kerrek SB Mar 10 '16 at 19:48
  • 1
    You can have dangling references similarly as dangling pointers - see http://stackoverflow.com/questions/4270902/dangling-reference-alternatives-for-dangling-pointers-and-references Though I never heard about dangling variables ;) – PiotrNycz Mar 10 '16 at 20:03

5 Answers5

3

Yes, you are absolutely right. At the conceptual level you can think of a reference as just another name for the same variable (as Stroustrup says in TC++PL). And the other way around: you can think of any variable as just a reference in disguise - the name actually belongs to the reference, which is tied to same unnamed region of storage.

However, this is what it looks like at the conceptual level.

In practice it might be quite different. From the practical point of view, in general case references are implemented under the hood as pointers. This means that access through reference requires implicit run-time dereference of that pointer. This makes reference access perform slower that "ordinary" direct variable access.

Yet, in many cases the compiler might be smart enough to figure out what a reference is bound to and eliminate the pointer dereference, replacing it with direct access to target variable. In that case the above concept ("reference as just another name for the variable") gets fully realized in practice.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

There is a way to distinguish reference from non-reference: when decltype is applied to name, it will yield type which that name is declared to have:

template<typename T>
class foo; //Incomplete type to force compiler error

int main()
{
    int i;        
    int& j = i;

    foo<decltype(i)> x;
    foo<decltype(j)> y;
}

error: aggregate foo<int> x has incomplete type and cannot be defined
error: aggregate foo<int&> y has incomplete type and cannot be defined

Revolver_Ocelot
  • 8,609
  • 3
  • 30
  • 48
1

So, other than the differing syntax of declaration, is there any difference between thisIsAReference and someVariable?

Not regarding the resolved value for read access, or the resolved address for writing.

You can actually think of thisIsAReference as an alias of someVariable.

Then basically a pointer is created to the location of someVariable, and that pointer can never point to another location and is always dereferenced.

No, that's an unspecified implementation detail, but could be used as a mind mapping model though.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

After

int& thisIsAReference = someVariable;

there is no difference, between thisIsAReference and someVariable. The reference is an alias, i.e. whenever you write someVariable you can as well write thisIsAReference. However, references are not "the same thing as a variable", because

int thisIsNotAReference = someVariable;
thisIsNotAReference = 20; // someVariable still has the old value

while with a reference

int& thisIsAReference = someVariable;
thisIsAReference = 20;    // now someVariable == 20 
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

You're right that a variable refers to a value at a specific location, and that a pointer will refer to the address of the location. One unique thing about references as opposed to pointers, is that you will never have a NULL value. A reference has to be initialized upon creation, as well that once it is initialized, it can't be changed. That is a huge difference compared to variables, because I can constantly change the value of a variable and that's what makes variables so useful.

Trey50Daniel
  • 179
  • 3
  • 14