A reference is simply another name, a synonym, an alias, for an object which already exists in memory. If, for example, I have an object, a variable of integer type which is called <i>val</i> and which contains the value 1. I can give another name, for example <i>x</i>, for this same area, for this same variable in memory. This is a reference. A reference is simply used to refer to another object in memory. This is exactly what we used when we passed arguments by reference. If, for example, we had a function <i>f</i> which took an integer a by reference, which we wrote like this, with the & symbol here, we were passing this argument by reference. If, somewhere else in the program I defined an integer, for example, named <i>b</i> and initialized to 3, and that I called <i>f(b)</i>, then <i>b</i> was passed by reference to the function <i>f</i> through the variable <i>a</i>, a parameter of function <i>f</i>. This passing by reference means that it is the same object that is referred to inside and outside the function. This is what it means to pass by reference. The general syntax for declaring a reference is the following. We can generalize this syntax, used for passing arguments by reference to functions, by adding a & symbol after the type and the name of the reference. It is not a new variable. It is just a new label for a variable which already exists. Here, we link it to an identifier. If I go back to a previous example, with a variable named <i>val</i> of int type, containing a value of 1. Now, I can declare a reference which I will call <i>x</i> and which is just another name for the variable <i>val</i>. This declaration, this syntax, means that <i>x</i> is another name for the variable <i>val</i> It does not create a new variable <i>x</i> in which I would copy <i>val</i>, but it creates a new flag, a new name, a new label, for something that already exists in memory, that is, the variable <i>val</i>. From now on, I can refer to this variable by the name <i>x</i> or by <i>val</i>. These two names will indicate the same variable. So be careful of possible confusions and traps linked to the fact that a reference is not a variable. It is just a label on a variable. For example, the <i>=</i> sign can be a trap. Let's compare two fairly similar pieces of code where we have a variable <i>i</i> initialized to 3 and a reference <i>j</i> which is a reference, an alias of <i>i</i>, and the other case, where we have a second variable <i>j</i>. What will happen? In both cases, we have a variable <i>i</i> which is initialized and contains a value of 3. In the case of the reference, we also have a reference <i>j</i>, that is, another name for the same variable, for the same memory area. In the right-hand side case, we have a variable <i>j</i> which is very different from the other case. In this case, what will happen is that we initialize <i>j</i> with the value of <i>i</i>. We copy the value of <i>i</i> into <i>j</i>. Whereas in the left-hand side case, in the reference case, it is the same object, the same variable which is referenced. This means that if we assign 4 to i in the case of the reference, what will happen is that both <i>i</i> and <i>j</i> will have a value of 4. <i>j</i> will also have a value of 4. Whereas in the right-hand side case, we only have <i>i</i> here; once the copy has been done, this arrow should not be here anymore. The copy is over. So, it is <i>i</i> and only <i>i</i> which has a value of 4. <i>j</i> still has a value of 3. Similarly, if we modify <i>j</i>, in the reference case, modifying <i>j</i> also modifies <i>i</i>. So here, if I modify <i>j</i>, then <i>i</i> will also have a value of 6. Whereas in the case of two completely different variables, modifying <i>j</i> will not modify <i>i</i> and so <i>i</i> still has a value of 4. Second little trap that can often lead to confusion: the meaning of <i>const</i>. We have already insisted several times on the fact that <i>const</i> means that modifications are impossible through the object which we described as <i>const</i>. But this object can be modified from elsewhere. This is the case with references. Let's look at it with an integer <i>i</i> initialized with a value of 3. Here, we declare a <i>const</i> reference on <i>i</i> which we name <i>j</i>. In other words, we have defined another name, another flag, another label. Through this label -- I will draw it thicker -- we cannot modify the content. This doesn't mean that the content cannot be modified elsewhere. If I want to modify the content of this variable which is named i and j, for example, by an assignment with a value of 12, I will not be able to. This line will be forbidden by the compiler. It is forbidden because, here, <i>j</i> is <i>const</i>, which means that I cannot effect any modification through <i>j</i>. However, I can still modify this memory area through <i>i</i> and I am allowed to do: <i>i = 12</i>, which will put 12 in the memory area <i>i</i> which is also the memory area named <i>j</i>. <i>j</i> also has a value of 12. To summarize, it is important to realize that the keyword, the reserved word <i>const</i> applies to the object, to the name we are talking about and not necessarily to the object in memory behind it. It applies to the object only through this name and not to the object itself. I said in the introduction sequence that references were not real pointers as such, and that they were quite different objects. This is why, incidentally, they have a different name, "references". So I would like to insist on the specifics of these references. The first of these is that a reference must absolutely always be initialized. It always refers to an existing object. A pointer, on the other hand, could point to something that does not exist. Here, I declared, again, a variable <i>i</i> of integer type, and here, I declare a reference <i>ri</i> on the object, the variable <i>i</i>. Which means that it is another name for <i>i</i>. But I cannot declare a reference <i>rj</i> which would be linked to no object. This is invalid. To what is it a reference? We do not know what it refers to. We cannot declare references which refer to nothing. It would lose all its sense. Similarly, a reference can be linked only to one object. This is another big difference with pointers. Let's go back to our example with the variable <i>i</i>. Here is a reference <i>ri</i> which refers to this variable <i>i</i>. Here, I declare another variable <i>j</i> initialized to 2. I cannot use <i>ri</i>, which is a reference to <i>i</i>. It is already used. It refers to an object <i>i</i>. I cannot move the label to <i>j</i>. If I draw this, I have the variable <i>i</i> which contains a value, we do not know which, it has not been initialized. I attached the reference <i>ri</i> to it. Then I declared another variable <i>j</i> which I initialized to 2. I cannot move a reference, it isn't possible. We are not allowed to do this sort of thing. However, this line, <i>ri = j</i>, compiles. It is quite legitimate in C++. What does this do now? Let's go back to the fundamentals. What is <i>ri</i>? It is another name for <i>i</i>. It is as if I had written <i>i = j</i>. If I had written <i>i = j</i>, what would have happened? We would have copied the value of <i>j</i> into <i>i</i> and so here, in the same way, we will copy the value of <i>j</i> into <i>ri</i>. So this line here, <i>ri = j</i>, does not mean that we move the label of <i>ri</i> to <i>j</i> and that we have another reference on the object <i>j</i>. It mean that we are copying the object <i>j</i> over to the object which is named <i>ri</i>. Proof: if I modify the value of <i>j</i>, for example if I give it a value of 3, then if I display the value of <i>i</i> you will see what will be displayed... I encourage you to test this program on your end. You will see that it will display 2. 2 because it is the value which we assigned to the variable, which is named both <i>i</i> and <i>ri</i>. Third specificity of references: one cannot refer to a reference. A reference is not an object in memory. It is just an additional label on an object in memory. So we cannot get its address, we cannot refer to such an object. If I go back once again to our example with the variable <i>i</i> which I initialize to 3, and to which I assign another name. If I take a reference named <i>ri</i> which refers to <i>i</i>. What we are asking ourselves is, can I take a reference on the reference <i>ri</i>? Well, no. <i>ri</i> is not an object as such. We cannot say that we will take a reference on a reference. This does not refer to <i>ri</i> as pointers would do, but actually to i, to the variable <i>i</i>. Even if this line compiles and makes <i>ri</i> another reference to <i>i</i>, it does not do what we wanted it do to originally. That is, a reference on the label <i>ri</i> itself. Even if I changed the type to say that I wanted a reference on a reference, this wouldn't work. We are not allowed to do that either. It is just as wrong, since this label <i>ri</i> does not really exist. It is not an actual variable in memory. <i>ri</i> is just another name for a variable which already existed. The consequence of all this, is that we cannot make arrays of references. We cannot put references into an array. I have an advanced remark however: the syntax <i>int&&</i> with two ampersands exists since C++2011, but it is not at all a reference on a reference. It is a much more advanced concept introduced in this new standard which is an rvalue reference. This is much too advanced for this course and will not be covered here.