8.14. Wrap-Up
In this chapter we provided a detailed
introduction to pointers, or variables that contain memory addresses as their
values. We began by demonstrating how to declare and initialize pointers. You
saw how to use the address operator (&) to
assign the address of a variable to a pointer and the indirection operator
(*) to access the data stored in the variable
indirectly referenced by a pointer. We discussed passing arguments by reference
using both pointer arguments and reference arguments.
You learned how to use const with pointers to enforce the principle of least
privilege. We demonstrated using nonconstant pointers to nonconstant data,
nonconstant pointers to constant data, constant pointers to nonconstant data,
and constant pointers to constant data. We then used selection sort to
demonstrate passing arrays and individual array elements by reference. We
discussed the sizeof operator, which can be
used to determine the sizes of data types and variables in bytes during program
compilation.
We demonstrated how to use
pointers in arithmetic and comparison expressions. You saw that pointer
arithmetic can be used to jump from one element of an array to another. You
learned how to use arrays of pointers, and more specifically string arrays
(arrays of strings). We discussed function pointers, which enable programmers to
pass functions as parameters. We introduced several C++ functions that
manipulate pointer-based strings. You learned string-processing capabilities
such as copying strings, tokenizing strings and determining the length of
strings.
In the next chapter, we begin our
deeper treatment of classes. You'll learn about the scope of a class's members,
and how to keep objects in a consistent state. You'll also learn about using
special member functions called constructors and destructors, which execute when
an object is created and destroyed, respectively, and we'll discuss when
constructors and destructors are called. In addition, we'll demonstrate using
default arguments with constructors and using default memberwise assignment to
assign one object of a class to another object of the same class. We'll also
discuss the danger of returning a reference to a private data member of
a class.