Learntofish's Blog

A blog about math, physics and computer science

References in C++

Posted by Ed on August 1, 2009

Last time I wrote about pointers in C++. Today I’d like to talk about references in C++. Let’s view the following source code:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int a = 5;
    int b = a;
    b = 7;

    cout << "The value of a is " << a << endl;
    cout << "The value of b is " << b << endl;

    system("PAUSE");
    return 0;
}
&#91;/sourcecode&#93;

As expected, the output is
<code>The value of a is 5</code>
<code>The value of b is 7</code>
We note that by assigning the value 7 to b the value of a remains unaffected.

Now, let's make a little change in line 9 by adding the ampersand sign:


#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int a = 5;
    int& b = a;
    b = 7;

    cout << "The value of a is " << a << endl;
    cout << "The value of b is " << b << endl;

    system("PAUSE");
    return 0;
}
&#91;/sourcecode&#93;

What do you think will be the output? The console output is
<code>The value of a is 7</code>
<code>The value of b is 7</code>
Suddenly, the value of a is not 5 anymore. What happened? In line 10 we assigned the value 7 to b which seems to have affected the value of a too. This is because we have defined a <em>reference</em>. Here, we defined <em>b as a reference to a</em> by putting the &amp; sign behind int like this:
<code>int&amp; b = a;</code>
This means that b becomes just another name for a. It is an alias. Changing b automatically changes the value of a. 

We can check the address of both a and b:

cout << "Address of a is " << &a << endl;
cout << "Address of b is " << &b << endl;
&#91;/sourcecode&#93;

The console output is:
<code>Address of c is 0x22ff6c
Address of d is 0x22ff6c</code>
Interesting, both a and b have the same address. This confirms that b is just another name for a.

Note that in line 9 the reference b has to be initialized immediately, i.e. one has to write <code>int&amp; b = a;</code>. Thus <code>int&amp; b;</code> yields an error.

<strong>Exercises</strong>
1) Declare a double variable f and assign the value 29.3 to it. Declare a reference g such that changing the value of g also changes the value of f. Assign the value 47.1 to g.
a) Display the value of f and g on the console.
b) Check whether f and g have the same address.

2) Compile the following code:
<code>int k = 2;
int&amp; m;</code>
What do you notice?

<strong>Answers</strong>
1)

//Solution 1a):
double f = 29.3;
double& g = f;
g = 47.1;
cout << "The value of f is " << f << endl;
cout << "The value of g is " << g << endl;

//Solution 1b):
cout << "The address of f is " << &f << endl;
cout << "The address of g is " << &g << endl;
&#91;/sourcecode&#93;

2)
Compiling this code yields the error message:
<em>`m' declared as reference but not initialized</em>
As mentioned above references have to be inizialized immediately, i.e. we must write int&amp; m = k such that m is a reference to k:

int k =  2;
int& m = k;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: