Home > Store

Advanced C++ Programming Styles and Idioms

Register your product to gain access to bonus material or receive a coupon.

Advanced C++ Programming Styles and Idioms

Book

  • Sorry, this book is no longer in print.
Not for Sale

Description

  • Copyright 1992
  • Dimensions: 6-1/4" x 9-1/4"
  • Pages: 544
  • Edition: 1st
  • Book
  • ISBN-10: 0-201-54855-0
  • ISBN-13: 978-0-201-54855-6

  • Recipient of Computer Language 1991 JOLT Cola Productivity Award
  • One of JOOP Magazine's Top Ten Books of the Decade (three years in a row)
  • A 1992 Stacey's best-seller
  • Continued critical acclaim in October 1995 Dr. Dobb's

Written by an expert in C++, Advanced C++ Programming Styles and Idioms looks at the design features of the C++ programming language. Assuming a background in the syntax of C++, Coplien shows how to become an expert C++ programmer by learning the idioms of the language. His approach is organized around the abstractions that C++ supports: abstract data types, combining types in inheritance structures, object-oriented programming, and multiple inheritance. Using small, but rich examples, he shows how these abstractions can be combined to use the language effectively. Experienced C++ programmers will appreciate the comprehensive coverage of Release 3.0.



0201548550B04062001

Downloads

Source Code

Click below for Source Code related to this title:
Source Code

Extras

Web Resources

Click below for Web Resources related to this title:
Author's Web Site

Software Examples

Sample Content

Table of Contents



Preface.


1. Introduction.

C++: An Evolving Language.

Handling Complexity with Idioms.

Objects for the Nineties.

Design and Language.



2. Data Abstraction and Abstract Data Types.

Classes.

Object Inversion.

Constructors and Destructors.

Inline Functions.

Initialization of Static Data Members.

Static Member Functions.

Scoping and const.

Initialization Ordering of Global Objects, Constants, and Static Class Members.

Enforcement of const for Class Object Member Functions.

Pointers to Member Functions.

Program Organization Conventions.



3. Concrete Data Types.

The Orthodox Canonical Class Form.

Scoping and Access Control.

Overloading: Redefining the Semantics of Operators and Functions.

Type Conversion.

Reference Counting: Making Variables Use “Magic Memory.”

Operators new and delete.

Separating Initialization from Instantiation.



4. Inheritance.

Simple Inheritance.

Scoping and Access Control.

Constructors and Destructors.

Class Pointer Conversion.

Type Selector Fields.



5. Object-Oriented Programming.

C++ Run-Time Type Support: Virtual Functions.

Destructor Interaction and Virtual Destructors.

Virtual Functions and Scoping.

Pure Virtual Functions and Abstract Base Classes.

Envelope and Letter Classes.

Functors: Functions as Objects.

Multiple Inheritance.

The Inheritance Canonical Form.



6. Object-Oriented Design.

Types and Classes.

The Activities of Object-Oriented Design.

Object-Oriented Analysis and Domain Analysis.

Object and Class Relationships.

Subtyping, Inheritance and Forwarding.

Rules of Thumb for Subtyping, Inheritance, and Independence.



7. Reuse and Objects.

All Analogies Break Down Somewhere.

Design Reuse.

Four Code Reuse Mechanisms.

Parameterized Types, or Templates.

Private Inheritance: Does Inheritance Support Reuse?

Storage Reuse.

Interface Reuse: Variants.

Reuse, Inheritance, and Forwarding.

Architectural Alternatives for Source Reuse.

Generalizations on Reuse and Objects.



8. Programming with Exemplars in C++.

An Example: Employee Exemplars.

Exemplars and Generic Constructors: The Exemplar Community Idiom.

Autonomous Generic Constructors.

Abstract Base Exemplars.

Toward a Frame Exemplar Idiom.

A Word About Notation.

Exemplars and Program Administration.



9. Emulating Symbolic Language Styles in C++.

Incremental C++ Development.

Symbolic Canonical Form.

An Example: A General Collection Class.

Code and Idioms To Support Incremental Loading.

Garbage Collection.

Primitive Type Encapsulation.

Multi-Methods under the Symbolic Idiom.



10. Dynamic Multiple Inheritance.

An Example: A Multi-Technology Window System.

Caveats.



11. Systemic Issues.

Static System Design.

Dynamic System Design.



Appendix A: C in a C++ Environment.

Function Calls.

Function Parameters.

Function Prototypes.

Call-by-Reference Parameters.

Variable Number of Parameters.

Function Pointers.

The const Type Modifier.

Interfacing with C Code.



Appendix B: Shapes Program: C++ Code.


Appendix C: Reference Return Values from Operators.


Appendix D: Why Bitwise Copy Doesn’t Work.

Why Member-by-Member Copy Isn’t a Panacea.



Appendix E: Symbolic Shapes.


Appendix F: Block-Structured Programming in C++.

What is Block-Structured Programming?

Basic Building Blocks for Structured C++ Programming.

An Alternative for Blocks with Deeply Nested Scopes.

Implementation Considerations Block-Structure Video Game Code.



Index. 0201548550T04062001

Preface

This book is designed to help programmers who have already learned C++ develop their programming expertise. To understand how programmers achieve proficiency, we need to understand not only how people learn a new language (such as a programming language), but also how a language is used to solve software problems effectively.

Learning Programming Languages

Not everything you need to know about a product is described in the owner's manual. Before the arrival of our first child, my wife and I were admonished by a friend that no book, and no training, could completely prepare us for the art of parenting. We must of course learn minimal, essential skills. But the interesting, challenging, and rewarding aspects of raising a child go beyond this basic knowledge. For example, no book or "owner's manual" will help you understand why your three-year-old daughter rubs toothpaste in your one-year-old's hair, or why your children hang their socks in the refrigerator.

The same is true of programming languages. Programming language syntax shapes our thinking to a degree, but what we learn in the "owner's manual" about syntax alone only gets us started. Most of what guides the structure of our programs, and therefore of the systems we build, is the styles and idioms we adopt to express design concepts.

Style distinguishes excellence from accomplishment. An effective parenting style, or programming style, comes from personal experience or by building on the experience of others. A software engineer who knows how to match a programming language to the needs of an application, writes excellent programs. To achieve this level of expertise, we need to go beyond rules and rote, into convention and style, and ultimately into abstractions of concept and structure. It is in that sense that this book is "advanced."

The rules, conventions, and concepts of programming drive the structure of the systems we build: They give us a model of how to build systems. A model for problem decomposition and system composition is a paradigm, a pattern for dividing the world into manageable parts. C++ is a multiparadigm language. C programmers use C++ as a better C. Object-oriented advocates do everything polymorphically. In fact, a variety of approaches is usually necessary to express the solution to a software problem efficiently and elegantly.

Learning a programming language is much like learning a natural language. Knowledge of basic syntax lets a programmer write simple procedures and build them into nontrivial programs, just as someone with a vocabulary of a few hundred German words can write a story far richer than see-Dick-run. But mastery of language is quite another issue. That such stories are nontrivial does not make them elegant or demonstrate fluency. Learning language syntax and basic semantics is like taking a 13-hour course in German: It prepares you for the task of ordering a bratwurst, but not for going to Germany to make a living, and certainly not for getting a job as a German language journalist or poet. The difference is in learning the idioms of the language. For example, there is nothing in C itself that establishes

   while (*cp1++ = *cp2++);

as a fundamental building block, but a programmer unfamiliar with this construct would not be perceived as a fluent C programmer.

In programming, as in natural language, important idioms underly the suitability and expressiveness of linguistic constructs even in everyday situations. Good idioms make the application programmer's job easier, just as idioms in any language enrich communication. Programming idioms are reusable "expressions" of programming semantics, in the same sense that classes are reusable units of design and code. Simple idioms (like the while loop above) are notational conveniences, but seldom are central to program design. This book focuses on idioms that influence how the language is used in the overall structure and implementation of a design. Such idioms take insight and time to refine, more so than the simple notational idioms. The idioms usually involve some intricacy and complexity, details that can be written once and stashed away. Once established, programming language idioms can be used with convenience and power.

The Book's Approach

Assuming a background in the basic syntax of C++, this book imparts the proficiency that expert C++ programmers gain through experience by giving a feel for the styles and idioms of the language. It shows how different styles let C++ be used for simple data abstraction, full-fledged abstract data type implementation, and various styles of object-oriented programming. It also explores idioms that the core of the C++ language does not directly support, such as functional and frame-based programming, and advanced garbage collection techniques.

The Book's Structure

Rather than taking a "flat" approach to learning the advanced features of C++ by organizing around language features, this book looks at these increasingly powerful abstractions from the perspective of the C++ features required to support them. Each chapter of this book is organized around a family of such idioms. The idioms progressively build on each other in successive chapters.

Chapter 1 provides a historical perspective on C++ idioms. It provides some motivation as to why idioms came about, and varying degrees to which different idioms can be thought of as part of the language or as outside the language.

Chapter 2 introduces the fundamental C++ language building blocks: classes and member functions. Though much of the material is basic, the chapter establishes idioms and vocabulary that recur in later chapters. It introduces compiler type systems, and their relationship to user-defined types and classes, from a design perspective. It also presents idioms that make const more useful.

Chapter 3 introduces idioms that make classes "complete" types. C++ has been evolving to automate more and more of the work of copying and initializing objects, but programmers still need to customize assignment and default constructors for most nontrivial classes. This chapter provides a framework for that customization. I call the idioms described in this chapter canonical forms, meaning that they define principles and standards to make the underlying mechanics of objects work. In addition to the most commonly used canonical form, idioms are presented to apply reference counting to new and existing classes. These are the first idioms of the book to go beyond straightforward application of the base C++ syntax. A variation on reference counting, counted pointers, is shown as a way to move C++ a step further away from the machine, abandoning pointers in deference to smarter, pointer-like objects. Lastly, the chapter looks at how to separate the creation of an object from its initialization. To someone familiar with basic C++, this might seem an unnatural idiom: C++ tightly couples these two operations. The need to separate them arises in the design of device drivers and in systems with mutually dependent resources.

Chapter 4 introduces inheritance; Chapter 5 adds polymorphism to inheritance to introduce object-oriented programming. Many new C++ programmers get "inheritance fever," using it at every occasion. While it is true that inheritance is used mostly to support the object paradigm, it has a distinctly separate application for software reuse. Introducing inheritance apart from polymorphism helps the reader separate the two concepts and avoids the confusion that often arises from trying to internalize two foreign concepts at once.

Chapter 6 approaches the constructs, styles and idioms of C++ from the perspectives of architecture and design. It examines what classes mean at the level of an application, high above the level of syntax. Appreciating the relationships between the design abstractions of an application, and between the classes and objects of its implementation, leads to systems that are robust and easily evolved. Another key to evolution is broadening designs beyond a specific application, to cover applications for a whole domain; guiding principles for domain analysis are an important part of this chapter. The chapter contains numerous rules of thumb about appropriate use of inheritance, an area of difficulty for inexpert C++ programmers. Readers who have been exposed to object-oriented design will appreciate the explanation in this chapter of how to transform the output of design to C++ code. Encapsulation as an alternative to inheritance, both for reuse and for polymorphism, is explored in the context of the C++ language.

Chapter 7 explores reuse of code and designs. Four distinct code mechanisms are explored, with particular attention devoted to the benefits and pitfalls of "inheritance fever." Idioms are presented to significantly reduce the code volume generated by parameterized type libraries using templates.

The remainder of the book stretches beyond native C++ into advanced programming idioms. Chapter 8 introduces exemplars, objects that take over many of the roles of C++ classes. Exemplars are presented as special objects that solve some common development problems, such as the "virtual constructor" problem. But exemplars also lay the groundwork for more powerful design techniques supporting class independence and independent development.

Chapter 9, which focuses on symbolic language styles, breaks with concepts many hold fundamental to C++ programming including strong typing and explicit memory management. The idioms of this chapter are certainly outside mainstream C++ development and are reminiscent of styles found in Smalltalk and Lisp. One might claim that those who want to program in Smalltalk should program in Smalltalk. Those who want Smalltalk in all of its glory should indeed use Smalltalk. However, the fact that the styles presented in this chapter are exotic does not mean that the need for them is rare or esoteric. Sometimes we want a small portion of a system to have the flexibility and polymorphism of symbolic languages, and in those situations we need to step outside the bounds of the C++ philosophy while working in the confines of the C++ semantics and type system. This chapter regularizes such idioms so they do not have to be created from scratch each time they are needed.

Chapter 9 also presents idioms supporting incremental run-time update. Implementations of this idiom are necessarily dependent on many details of the target platform. The gist of this material is to familiarize the reader with the level of technology at which incremental loading issues must be worked. The example presented is typical and, as such techniques go, it is neither obtuse nor trivial. The code presented for incremental loading needs major reworking for platforms other than Sun work stations, and it may be found altogether unsuitable to some environments. None of the book's other idioms depend on this idiom, so it can be pursued or rejected on its own merits. The goal of Chapter 9 is not to change C++ into Smalltalk; this cannot, and should not, be done. These idioms are less compile-time type safe and generally less efficient than "native C++" code; what they offer is flexibility and an increased measure of automated memory management.

Chapter 10 covers dynamic multiple inheritance. Multiple inheritance is a controversial C++ feature, and discussion of this dynamic variation is separated out to avoid tainting other chapters. While static multiple inheritance as described in Chapter 5 has value, dynamic multiple inheritance avoids problems of a combinatorial explosion of class combinations. This approach has been found valuable in many real-life programs including editors, CAD systems, and database managers.

The last chapter discusses objects from a high-level, system view. The chapter raises the level of abstraction above chunks the size of a C++ class, to larger and more encompassing units of software architecture, organization, and administration. The chapter puts a number of important system issues in perspective, including scheduling, exception handling, and distributed processing. Some guidelines for modularization and reuse are also discussed, tying together the concepts of Chapters 6 and 7. Included in this discussion are considerations for library structure and maintenance.

In Appendix A, the basic C++ concepts are compared with their C analogues. Many readers will have already learned these basics or can find them in introductory texts. This material is included here for two reasons. First, it serves as a ready reference for those occasions when you need clarification of an obscure construct without having to go to a separate text. Second, C and C++ styles are viewed from a design perspective, showing how to mix procedural and object-oriented code. This is particularly important for C++ programmers working with a base of C code.

The examples in this book are based on Release 3.0 of C++, and have been tried under Release 3 of the AT&T USL Language System on many different hardware platforms, and under some other C++ environments as well. Many of the examples have been tried under GNU C++ Version 1.39.0 and Zortech C++ 2.0, though examples using features of the 3.0 release await forthcoming releases of these tools. Some code makes use of general purpose class libraries for maps, sets, lists, and others. Efficient versions of such libraries are available from many vendors, and adequately functional versions can be created from scratch for pedagogical purposes. The skeletons, and sometimes complete bodies, of many general-purpose classes can be gleaned from examples in the book. Key class names are listed in the Index.

Acknowledgments

This book owes much to many friends. The original impetus for the book came from Chris Carson at Bell Laboratories, and he if anyone is the book's godfather. I appreciate his initiative and support in starting this effort. The book reflects the guidance of Keith Wollman, my resourceful and accommodating editor, and Helen Wythe, the production supervisor. Lorraine Mingacci collaborated significantly with me on Chapter 6 and discussions with her about the rest of the book have been of immense value. The book owes much to a core team of thorough and relentless reviewers and contributors of ideas: Tim Born, Margaret A. Ellis, Bill Hopkins, Andrew Koenig, Stan Lippman, Barbara Moo, Bonnie Prokopowicz, Larry Schutte, and Bjarne Stroustrup. Alexis Layton, Jim Adcock, and Mike Ackroyd offered suggestions and insights to keep the book focused and balanced, and I am deeply grateful for their contributions. Many other improvements are owed to reviews by Miron Abramovici, Martin Carroll, Brian Kernighan, Andrew Klein, Doug McIlroy, Dennis Mancl, Warren Montgomery, Tom Mueller, Anil Pal, Peggy Quinn, and Ben Rovegno. Mary Crabb, Jean Owen, and Chris Scussel lent their expertise on the wonders of text formatting. Brett L. Schuchert and Steve Vinoski took painstaking effort to report the bugs that made it into early printings; their efforts have greatly improved the quality of subsequent printings. Credit goes to Corporate Desktop Services of Glen Ellyn, Illinois, for working out the final phototypesetting bugs. Many thanks to Judy Marxhausen for advice on special topics.

Much credit goes to AT&T managers for their encouragement and for their provision of time and resources to work on the book. Thanks to Paul Zislis and Ben Rovegno for support early on, and very special thanks to Warren Montgomery, Jack Wang, and Eric Sumner, Jr., for their support, ideas, and forbearance.

Students from many of my courses have been a good source of input and feedback on materials that would later be brought together in this book. Special thanks go to the students of the C++ courses I taught at AT&T Bell Laboratories, both in Naperville, Illinois and in Columbus, Ohio, in 1989.



0201548550P04062001

Updates

Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020