3

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.

BЈовић
  • 14,049
Chris
  • 161

4 Answers4

7

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.

Jon Purdy
  • 20,597
2

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

Doc Brown
  • 218,378
1

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.

DeadMG
  • 36,914
1

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.