C++: References and Pointers

References and Pointers

This video tutorial covers one of the core areas of C++: references and pointers. Despite the seeming obscurity of this material, understanding it is absolutely essential and we will be making heavy usage of references and pointers in the coming lessons.

A reference gives us another name for something, kind of like how Superman and Clark Kent are two names for the same man. Typically, the name will be highly dependent on the context. In the case mentioned, for instance, we would refer to the man in question as Superman when he is fighting crime and Clark Kent when he is . . . um, doing whatever it is that Clark Kent does.

In the coming lessons, we will explore many of the uses for references and pointers. In the meantime, we will give a few some simple examples here.




We can use references to make our code cleaner. This example uses entries from an array which holds the number of movie tickets sold for each week and the ticket price to calculate the total number of movie tickets sold and the total sales for the year. The repeated use of the array entry in calculations makes lines that are excessively long and unreadable. Here, we wrapped the lines to better fit them on the page.

Using references, we can shorten these lines of code and make them more readable like this:

The local variable is much clearer in context, and we have tightened the code somewhat. In fact, by using a constant reference for the number of movie tickets sold during the week we have enforced the rule that value will not be changed. Generally, we will always want to make a value constant if we have no intention of changing it; doing this, we make the compiler enforce proper coding.




Pointers are extremely powerful. In fact, references were created in C++ as a less powerful alternative to pointers to enforce good coding, as we did above by our making value constant. The full power of pointers will become apparent as we cover more C++, but we will settle for a somewhat contrived example at present.

In this somewhat contrived example, we are considering two alternative careers based on salary: a scientist and a computer programmer. Ideally, we would like the largest salary. So, we choose that career and calculate the gross income and net income, assuming a flat 20 percent tax rate. Our code looks like this:

Above, we have two blocks in if statement which do the same calculations on two different values. If we imagine that these blocks of code were more than simply two lines to calculate the gross income and the net income, than we can see how a pointer might reduce the code significantly by doing this:

The number of lines of code in this example is the same for either code segment. However, if we were calculating much more than just gross income and net income, then using a pointer like this would cut the code in half, approximately. Using a pointer like this makes our code less dependent on our chosen career. The added level of abstraction helps to facilitate code reuse.