Online Book Reader

Home Category

Professional C__ - Marc Gregoire [178]

By Root 1099 0
presents much more detail on the subject of operator overloading, but those details are not required to understand the following chapters.

OVERVIEW OF THE C++ STANDARD LIBRARY


This section introduces the various components of the standard library from a design perspective. You will learn what facilities are available for you to use, but you will not learn the coding details. Those details are covered in other chapters throughout the book.

Note that the following overview is not comprehensive. Some details are introduced later in the book where they are more appropriate, and some details are omitted entirely. The standard library is too extensive to cover in its entirety in a general C++ book; there are books with over 800 pages that cover only the standard library.

Strings

C++ provides a built-in string class. Although you may still use C-style strings of character arrays, the C++ string class is superior in almost every way. It handles the memory management; provides some bounds checking, assignment semantics, and comparisons; and supports manipulations such as concatenation, substring extraction, and substring or character replacement.

Technically, the C++ string is actually a typedef name for a char instantiation of the basic_string template. However, you need not worry about these details; you can use string as if it were a bona fide nontemplate class.

In case you missed it, Chapter 1 briefly reviews the string class functionality , and Chapter 14 provides all the details.

I/O Streams

C++ introduces a new model for input and output using streams. The C++ library provides routines for reading and writing built-in types from and to files, console/keyboard, and strings. C++ also provides the facilities for coding your own routines for reading and writing your own objects. Chapter 1 reviews the basics of I/O streams. Chapter 15 provides the details of streams.

Localization

C++ also provides support for localization. These features allow you to write programs that work with different languages, character formats, and number formats. Chapter 14 discusses localization.

Smart Pointers

One of the problems faced in doing robust programming is knowing when to delete an object. There are several failures that can happen. A first problem is not deleting the object at all (failing to free the storage). This is known as memory leaks, where objects accumulate and take up space but are not used. Another problem is where someone deletes the storage but others are still pointing to that storage, resulting in pointers to storage which is either no longer in use or has been reallocated for another purpose. This is known as dangling pointers. One more problem is when one piece of code frees the storage, and another piece of code attempts to free the same storage. This is known as double-freeing. All of these tend to result in program failures of some sort. Some failures are readily detected; others simply cause the program to produce erroneous results. Most of these errors are difficult to discover and repair.

C++11 introduces two new concepts, the unique_ptr and the shared_ptr, which attempt to address these problems.

The unique_ptr is analogous to an ordinary pointer, except that it will automatically free the memory or resource when the unique_ptr goes out of scope or is deleted. One advantage of the unique_ptr is that it simplifies coding where storage must be freed when an exception is taken. When the variable leaves its scope, the storage is automatically freed.

shared_ptr allows for distributed “ownership” of the data. Each time a shared_ptr is assigned, a reference count is incremented indicating there is one more “owner” of the data. When a shared_ptr goes out of scope, the reference count is decremented. When the reference count goes to zero it means there is no longer any owner of the data, and the object referenced by the pointer is freed.

Before C++11, the functionality of unique_ptr was handled by a type called auto_ptr, which is now deprecated and should not be used anymore. There was no equivalent

Return Main Page Previous Page Next Page

®Online Book Reader