Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In the current semester at the university we are working on OOP with C++.
I would like to understand the difference between a pointer and a reference operator.

The differences that I understand are:
1. Cannot change the object that the reference variable is binded to
2. we can use the reference variables to refer to the binded object without having to type the & operator (in contrast with the pointers where we would write *pi = 5;)

Also,does a reference variable contain the address of the object that is binded to?
In example:

int i;
int &ri = i; 

Here ri contains the address of i?

And the reason why when overloading ++ operator in this example of enumeration we are using the dereference or reference(*) operators before the name of the function.

share|improve this question
add comment

4 Answers

up vote 5 down vote accepted

A reference serves one purpose: to act as an alias for an existing variable:

int i;
int &ri = i;

In this example, the addresses of i and ri are identical (&i == &ri). The two names refer to the very same location in memory. Thus one of the useful applications of references is to provide an alias for a long name that it would be wasteful to retype:

const Location& p = irresponsibly_long_object_name.retrieve_location();

When you pass a function parameter by reference, it has the same effect:

void increment(int& parameter) { ++parameter; }

int main(int argc, char** argv) {
    int variable = 0;
    increment(variable);
}

Here, parameter serves merely as an alias of variable, just as though it were defined as int& parameter = variable. Modifying parameter inside increment() is indistinguishable from modifying variable in main(). Furthermore—and this is quite useful—variable does not need to be copied; so if it were of a large type such as std::list<int>, then passing it by reference would be much more efficient. That’s the main point of const references.

For the purpose of function parameters of reference type, the sanest course of action for a compiler is to use a pointer internally, as if you had written:

void increment(int* parameter) { ++*parameter; }

int main(int argc, char** argv) {
    int variable = 0;
    increment(&variable);
}

This is an interesting detail, and well worthwhile to remember. While it is not essential to your understanding of references, recognising this equivalence allows you to think of pointers merely as explicit references. Likewise, references are implicit pointers.

share|improve this answer
    
Slight concern about "one purpose". A common use is by-reference parameters. They are aliases, but that isn't really how people think of them - or at least not how I think of them anyway. Pass-by-reference is a purpose in itself, and using an "alias type" is in that case just the means. In some languages (e.g. Pascal family), there are no C++-style reference types but pass-by-reference parameters are still supported. –  Steve314 Dec 25 '11 at 5:05
    
@Steve314: I guess that’s a point to clarify. I meant of course that the “purpose” (function) of references in C++ is to alias, but to what “purpose” (end) you apply that feature naturally varies. –  Jon Purdy Dec 25 '11 at 5:29
add comment

Technically, there are not much differences between variable of type int* and int& (except those syntactical details about dereferencing and member access). The main difference in practical use IMHO is that you cannot change the object a reference is refering to after initializiation (while you can change the adress a pointer points to), and a reference cannot be NULL (at least, not without some ugly C++ casting hacks).

Here is a comprehensive explanation of the topic:

http://www.parashift.com/c++-faq-lite/references.html#faq-8.2

share|improve this answer
    
The '->' and '.' are the dereference operators and accessing members of a class(or struct) of '*' and '&' respectively? –  Chris Dec 24 '11 at 19:18
    
@chris: what I wrote was not fully correct, so I changed my answer a little bit. Please read the article if you need more detailed information. –  Doc Brown Dec 24 '11 at 22:48
    
There is no legal way to make a NULL reference. Any method you find is undefined behavior and thus not a real way of doing it. –  Loki Astari Dec 25 '11 at 1:48
add comment

Technically speaking, references are not objects and you cannot reason about their contents. However, in reality, as pointers under the hood that's how they're often implemented.

As for the function declaration/definition, they are not operators. They are syntax. int& is a type. It means "I return a reference". Returning an int& isn't any different to returning an int- you return a thing of that type.

Of course, it's double use as syntax and an operator isn't helpful, but there is no operating going on here.

share|improve this answer
    
Regarding the function declaration/definition question, forget I aksed, I know that functions return a certain type. So, references are pointers under the hood as you said? How can I imagine these variables(references)? What I mean is with pointers we would think of a box that contains an address and points to another box(a variable). –  Chris Dec 24 '11 at 12:44
    
So, if there is not difference in returning int& and int why do we use the int& sometimes? Is it because when we want to write this: a=b=c? (when a,b,c are members of a class) –  Chris Dec 24 '11 at 12:53
    
@Chris: There is a difference in the type you returned, of course, but nothing more than that. You return the int& when you want people to be able to modify whatever it's bound to. A reference is a pointer, except sometimes, or even many times, the compiler optimizes it out of existence. –  DeadMG Dec 24 '11 at 16:32
1  
Thinking of it as a fancy pointer makes things harder. It is an alias. Think of it as another name for an already existing object. –  Loki Astari Dec 25 '11 at 1:44
    
@Chris - Your box that contains an address is exactly as (pedantically-not-quite) correct for references as pointers. References are also called (e.g. in Stroustrup I think) "self-dereferencing pointers". When you use a reference, it is automatically dereferenced so you don't need the "*" dereference operator. It's therefore no use if you need to change that pointer, which is why it's basically used as an alias and (mostly) not for data structures. It's kinda like Pascal "var" parameters (and often used for the same job), but can be "const" and isn't restricted to being used for parameters. –  Steve314 Dec 25 '11 at 4:52
show 1 more comment

A reference is another name(alias) for an existing object; it gives you the power of "pass by reference" and the convenience of using it like you would the object itself.

In your example, the address of ri and i are the same and the values of each are the same. How the compiler handles this is another topic, but it really does boil down to my first sentence.

share|improve this answer
    
So all in all they are the same object, such as an another variable which gives us access to the value of i, right? –  Chris Dec 24 '11 at 19:15
    
@Chris yes. It is just another name for an existing object. If you try and take the address of ri you actually get the address of i for exactly that reason (it is just an alias). ri is not an object in its own right. –  Loki Astari Dec 25 '11 at 1:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.