This chapter from Bjarne Stroustrup's Programming: Principles and Practice Using C++, 2nd Edition describes how vectors are copied and accessed through subscripting.



“Caveat emptor!”

—Good advice

This chapter describes how vectors are copied and accessed through subscripting. To do that, we discuss copying in general and consider vector’s relation to the lower-level notion of arrays. We present arrays’ relation to pointers and consider the problems arising from their use. We also present the five essential operations that must be considered for every type: construction, default construction, copy construction, copy assignment, and destruction. In addition, a container needs a move constructor and a move assignment.

18.1 Introduction

18.2 Initialization

18.3 Copying 18.3.1 Copy constructors 18.3.2 Copy assignments 18.3.3 Copy terminology 18.3.4 Moving

18.4 Essential operations 18.4.1 Explicit constructors 18.4.2 Debugging constructors and destructors

18.5 Access to vector elements 18.5.1 Overloading on const

18.6 Arrays 18.6.1 Pointers to array elements 18.6.2 Pointers and arrays 18.6.3 Array initialization 18.6.4 Pointer problems

18.7 Examples: palindrome 18.7.1 Palindromes using string 18.7.2 Palindromes using arrays 18.7.3 Palindromes using pointers



18.1 Introduction

To get into the air, a plane has to accelerate along the runway until it moves fast enough to “jump” into the air. While the plane is lumbering along the runway, it is little more than a particularly heavy and awkward truck. Once in the air, it soars to become an altogether different, elegant, and efficient vehicle. It is in its true element.

In this chapter, we are in the middle of a “run” to gather enough programming language features and techniques to get away from the constraints and difficulties of plain computer memory. We want to get to the point where we can program using types that provide exactly the properties we want based on logical needs. To “get there” we have to overcome a number of fundamental constraints related to access to the bare machine, such as the following:

An object in memory is of fixed size.

An object in memory is in one specific place.

The computer provides only a few fundamental operations on such objects (such as copying a word, adding the values from two words, etc.).

Basically, those are the constraints on the built-in types and operations of C++ (as inherited through C from hardware; see §22.2.5 and Chapter 27). In Chapter 17, we saw the beginnings of a vector type that controls all access to its elements and provides us with operations that seem “natural” from the point of view of a user, rather than from the point of view of hardware.

This chapter focuses on the notion of copying. This is an important but rather technical point: What do we mean by copying a nontrivial object? To what extent are the copies independent after a copy operation? What copy operations are there? How do we specify them? And how do they relate to other fundamental operations, such as initialization and cleanup?

Inevitably, we get to discuss how memory is manipulated when we don’t have higher-level types such as vector and string . We examine arrays and pointers, their relationship, their use, and the traps and pitfalls of their use. This is essential information to anyone who gets to work with low-level uses of C++ or C code.