TweetFollow Us on Twitter

July 93 - James Coplien’s Advanced C++

James Coplien’s Advanced C++

Eric Rose

Advanced C++ is an excellent book for the C++ programmer who is interested in object-oriented programming.

Aspects of the traditional issues of software design and reuse in an object-oriented programming language are covered. But Coplien goes further, taking the model of more dynamic languages and applying it to C++. He shows how one can design conventions in C++ that support incremental development, garbage collection, runtime polymorphism, and dynamic object loading. The book is filled with a diversity of ideas and topics, programming techniques and idioms: food for thought for the interested reader.

First Impressions

One is confronted with a myriad of C++ books today. They sit on the bookshelf in the Computer Languages section of the bookstore, and most of them look the same. This problem seems to plague all technical books, especially those on programming languages. How can one evaluate one of many books about an unfamiliar language? This is the dilemma I was confronted with last year at the bookstore. After selecting Stroustrup's C++ book (the new edition),I wanted to have another reference book, so I picked one called Advanced C++ by James Coplien. After all, most language books seem to stop teaching the language just as it becomes interesting, so even if Coplien's book was slightly advanced, it would be a good choice. Besides, my employer was buying.

Later, as I read the book, I found that I became completely lost during the fifth chapter. Obviously this book would require a more thorough reading. Coplien's book is about C++, but it teaches object-oriented programming technique as well. With C++, Coplien ventures into many of the aspects of experimental, dynamic languages such as Smalltalk and Self. By carefully reading and re-reading the chapters, often pausing to ponder over the examples, I began to comprehend them. As I learned C++, chapters of the book began to make more sense. This is one characteristic of a good book-it grows with the reader.

TOPIC SUMMARY

Chapter 1, "Introduction," introduces the design philosophy of C++ as well as a short history. Coplien explains how C++ was conceived and how it grew over the years as it was used. Object-oriented programming has grown with C++, and there has always been a tension between keeping the language simple and adding new features. For example, rather than add new keywords many of the existing ones, such as static or virtual, were reused. This kept the number of keywords low, but added to the complexity of the compiler and made it harder for programmers to learn. Another tension in C++ is that of providing for the programmer's needs with language features without restricting the implementation of the language or the compiler.

Chapter 2, "Data Abstraction and Abstract Data Types," is a class-oriented introduction to C++. Classes, constructors and destructors, initialization, scoping, const, and pointers to members are introduced in this chapter. Many of the differences between C and C++ that are not strictly object-oriented in nature are reserved for Appendix A.

Chapter 3, "Concrete Data Types," describes how to write objects that behave as built-in data types. Scoping and access control, the semantics of overloading, operators, and type conversions are discussed. Coplien also introduces the idea of reference counting along with several implementations: using handle classes or counted pointer objects. Bootstrapping reference counting to an existing class is also covered. The orthodox canonical form, introduced in the beginning of the chapter, is a commonly used format for objects in C++.

Chapter 4, "Inheritance," is an explanation of single inheritance. Some of the semantics of object inheritance, such as the order of instantiation and initialization, member access (public/protected/private), pointer conversion, and passing parameters to base class constructors are considered. The concept of a virtual function is reserved for chapter 5; instead Coplien implements a set of classes with type selector fields. This provides a good contrast with the next chapter, where the same example is rewritten using virtual functions to eliminate the type member variable and corresponding case statements.

Chapter 5, "Object-Oriented Programming," a long chapter, introduces many new ideas. This is where the terminology becomes complicated. Coplien will introduce an idea with its term, such as a virtual function, and will go on to build even more abstract ideas from it. He spends several sections explaining the idea of a virtual function and its purpose, including some explanation of the runtime penalty for invoking one. Many other C++ books will gloss over this idea with a half-baked example. Advanced C++ treats the topic thoroughly and is careful to explain the implications and restrictions of virtual function calls.

Virtual destructors (why they are useful) and scoping are included. Coplien then uses the idea of a virtual function to introduce the idea of a pure virtual function as a mechanism to create an abstract base class. The Envelope/Letter idiom is reintroduced as a powerful way to extend the idea of a single class. By using two classes, one that contains and controls access to the other, more dynamic functionally can be achieved. It is reimplemented using the new concepts of inheritance and delegation using virtual functions.

The chapter continues by giving several implementations of virtual constructors. There is also an extension to the Envelope/Letter idiom to allow variable-sized objects, and one for delegation by overloading the "->" operator. Functors, objects that behave like functions (by overloading the "()" operator) are introduced, with a good example, and the chapter ends with a discussion of the uses and pitfalls of multiple inheritance. This is a good introduction to some of the problems created by allowing multiple inheritance in a flexible language such a C++. Casting a pointer to a multiply-inherited object can, in some situations, change the value of the pointer. This is a potentially nasty surprise for those native C programmers who are accustomed to loose pointer casting conventions.

Chapter 6, "Object-Oriented Design," introduces some of the theory involved in object-oriented programming. Object relationships and iconic representations for them are shown. There is an excellent discussion of some of the problems of using inheritance to solve type problems. Some designers will use inheritance not because it implies a type relationship, but to gain functionality for an unrelated object. Others will use inheritance with homonymic types, types that share a common set of operations but differ semantically (usually one has more restricted functionality than the other). Coplien distinguishes between proper and improper uses of inheritance, with good examples. Inheritance with addition and cancellation are considered. Coplien also brings up the topic of public data in objects, and relates it to the problems of inheritance with class independence. At the end of the chapter are rules of thumb for subtyping and inheritance .

Chapter 7, "Reuse and Objects," covers some of the software issues surrounding code reuse. Coplien stresses that object-oriented systems must be consciously designed for reuse. Four code reuse methods, from preprocessor macros to templates, are proposed. Often a system that is designed with reuse as its highest priority cannot fulfill the basic goals that were intended, so it is important to consider reuse, but perhaps not as the primary factor. At the end of the chapter are some code reuse generalizations. Reuse is not solely an object-oriented programming issue. In fact, code reuse is more dependent on good documentation and indexing facilities than the language used. A CASE system can enhance reusability for any large software project. While C++ provides several reuse mechanisms, such as inheritance, templates, and macros, reuse must be designed into the class hierarchy. Sometimes reuse and functionality are in conflict with one another. Also, good software engineers should consider the level of generality that the system will support. Class libraries are perhaps the best of example of a system designed with reuse as a principal goal. Stroustrup is quoted as saying "Code must be usable before it can be reusable."

Chapter 8, "Programming with Exemplars in C++," introduces the idea of an Exemplar. Exemplars allow more dynamic programming by making the class constructor private (or protected) and providing a "make" virtual function with a global "exemplar" object. To create a new object, one applies "make" to the exemplar. By using a virtual member function that is not a constructor, one can invoke make on any object to clone it, when only a pointer (possibly to a base class) is available. The exemplar can be either a global pointer or a class variable (static member). Exemplars can be used to simulate virtual constructors by having an Abstract Base Exemplar examine the data and return the appropriate new object. By moving all member functions signatures to the base class as virtual functions (Inclusion Polymorphism) one can handle objects by using pointers to their base classes. This simulates a more dynamic style of object referencing. Frame-based programming, in which messages are dispatched to subclasses using a single "doit" function, is more dynamic but is subject to performance problems and requires a good error recovery subsystem.

Inclusion Polymorphism is a good idea, but requires more software support to be feasible. Putting the member functions of subclasses in a common class make a system inflexible, and requires substantial recompilation when an interface is changed, making incremental development difficult. Coplien is aware of these problems and gives some program administration tips to help. A large project would require either strict conventions or a source preprocessor. It is important to remember that any software system in C++ will require style and use conventions. As one pushes C++ into the realm of the dynamic these conventions become important. Abstract base exemplars need some information about their subclasses to implement virtual constructors. The idea of an Autonomous Generic Constructor allows abstract base exemplars to keep a list of their subclasses and return the first one that can successfully construct itself from the input data.

Chapter 9, "Emulating Symbolic Language Styles with C++," describes how, by using Envelope/Letter classes with Exemplars to create what Coplien calls the symbolic canonical form, one can create a system that supports incremental development, dynamic loading and reloading, and garbage collection. Coplien provides system-dependent code that works on a Sun (SunOS 4.0, it seems) with the AT&T USL C++ Release 3 compiler. To support dynamic loading in C++ with objects, one needs procedures to load new virtual functions and to change object formats. All loadable functions must be virtual, because when the new one is loaded, one edits the virtual-function table (vtbl) and replaces the pointer to the old function with that of the new. One also needs a way of indexing the vtbl, so that the slot for each virtual member function is known. Loading an object with a new data format is more difficult. It requires that one keep a list of all objects and apply a conversion-function to them (called cutover) when the new object is loaded. There is also some trickiness with the order in which the operations are performed, and one must assure that cutover is applied only once for each object. Because the overhead to support these mechanisms can be cumbersome, one would only want to implement a system this way if the dynamic properties are important. Unfortunately, dynamic loading is implementation dependent.

Coplien also shows how to implement garbage collection using a combined mark-and-sweep and Baker's algorithm (semispace copying). The algorithm requires each class to allocate from a memory pool and each object to have mark and in use bits. When free memory becomes low, first the objects in the exemplar's master list are marked, then the memory pool is scanned and the unmarked objects in use are reclaimed. The disadvantage of this system is the fixed-size memory pools. The chapter ends with an implementation of multi-methods using the symbolic canonical form.

Chapter 10, "Dynamic Multiple Inheritance," is a short chapter about implementing multiple inheritance using pointers to base classes and delegation. Apparently this was one of the methods used before language support for multiple inheritance was added to C++.

Chapter 11, "Systemic Issues," is a collection of topics not directly related to the other chapters. There is a short discussion of modules, frameworks, and software libraries. "Dynamic System Design" is a section on the use of object in a multithreaded environment and the design issues this raises, such as error recovery, scheduling, separate name spaces, and inter-object communication. This is a good summary of the software design idioms that are useful in a multithreaded or multiprocessor context.

OVERALL IMPRESSIONS

Every object-oriented language has its own terminology. One of the difficulties in learning C++ is the vocabulary. Coplien makes frequent use of the terms specific to C++, but often mentions those used in other languages. It seems that he invents some of his own as well. Understanding the terminology is critical to understanding the ideas he presents. As a result, one quickly becomes lost by casually reading or "skimming" this book. Each chapter builds on the ideas and idioms of the previous chapters, and often will make reference to or reimplement idioms introduced earlier.

A major theme of Advanced C++ is the conflict of static typing with dynamic programming. Coplien is interested in how a C++ programmer can write code to simulate valuable features of dynamic object-oriented languages such as Self, Smalltalk, or CLOS. For example, the idea of a virtual constructor is prevalent throughout the book. A virtual constructor is an object constructor that evaluates the data provided and returns the appropriate object. Thus what type of object is created is not known until runtime. To accomplish this, a base class must have some information about its derived classes. Coplien shows several ways to implement the idea of a virtual constructor in C++, including Letter/Envelope classes and Abstract Base Exemplars.

Examples

One aspect of the book I like is that Coplien does not lock himself into one mode of solving problems. In the chapter on code reuse (chapter 7), he presents four different mechanisms to achieve software reuse. Examples are used well in this book. They are short enough to fit in a few pages, yet complex enough to warrant careful reading. The text makes interesting comments on the examples. Some programming books do not go into detail about the tradeoffs involved in the coding. Coplien usually explains what the limitations of his examples are. In the beginning of the book Coplien writes C and its equivalent C++ code side-by-side to show the usefulness of the C++ extensions. This works well to show the usefulness of classes over structs as well as constructors and destructors over C init/destroy functions. Longer examples are relegated to the ends of chapters. At the end of the book are several complete programs; however, none are too long or complicated to type in and run.

Appendices

Coplien uses his appendices to introduce code samples and for short tangential topics that are interesting but not related directly to object-oriented programming in C++. For example, Appendix D demonstrates some of the problems with bitwise copy of objects, and explains why member-by-member copying (sometimes called "deep" copying) is not always the correct solution.

Appendix A, "C in a C++ Environment" covers converting C programs to a style more like C++. Some of it involves the conversions necessary to take traditional Kernighan and Ritchie C to ANSI-C. (C++ is almost ANSI-C compliant.) It also covers how to use const, interfacing with C libraries, sharing header files between the two languages, any how names and object data formats are represented in a C environment. This chapter is a good summary of some of the non-Object-Oriented aspects of C++.

Appendix C, "Reference Return Values from Operators" clarifies the concept of a Reference, especially as a return value from an operator. References often confuse novice C++ programmers who are accustomed to pointers in C. Appendix F, "Block-Structured Programming in C++" explains how to write a C++ program using blocks, or scopes, such as in a Pascal or Modula-2 program.

Conclusions

A few weeks ago I found myself again in the computer science section of the bookstore. As I browsed through a software magazine, I noticed a book review, which stated that "Advanced C++ is a classic, a must-have on the shelf next to Stroustrup's C++ Primer or the Annotated C++ Reference Manual (ARM)".

I concur. For the serious C++ programmer the book is a must-have. It is full of interesting ideas and clever techniques that extend the power of the language. The only problem I see is that using any of these techniques for a large system will require some sort of preprocessing to aid in the generation of the support for each class. The section on writing dynamically loadable (and reloadable) objects demonstrates the programming skill and depth of understanding of the author. James Coplien is well-versed not only in C++ and its implementation, but in Object-Oriented theory and practice. I highly recommend this book. -

 
AAPL
$97.67
Apple Inc.
+0.64
MSFT
$44.50
Microsoft Corpora
+0.10
GOOG
$589.02
Google Inc.
-4.33

MacTech Search:
Community Search:

Software Updates via MacUpdate

Vienna 3.0.0 RC 2 :be5265e: - RSS and At...
Vienna is a freeware and Open-Source RSS/Atom newsreader with article storage and management via a SQLite database, written in Objective-C and Cocoa, for the OS X operating system. It provides... Read more
VLC Media Player 2.1.5 - Popular multime...
VLC Media Player is a highly portable multimedia player for various audio and video formats (MPEG-1, MPEG-2, MPEG-4, DivX, MP3, OGG, ...) as well as DVDs, VCDs, and various streaming protocols. It... Read more
Default Folder X 4.6.7 - Enhances Open a...
Default Folder X attaches a toolbar to the right side of the Open and Save dialogs in any OS X-native application. The toolbar gives you fast access to various folders and commands. You just click... Read more
TinkerTool 5.3 - Expanded preference set...
TinkerTool is an application that gives you access to additional preference settings Apple has built into Mac OS X. This allows to activate hidden features in the operating system and in some of the... Read more
Audio Hijack Pro 2.11.0 - Record and enh...
Audio Hijack Pro drastically changes the way you use audio on your computer, giving you the freedom to listen to audio when you want and how you want. Record and enhance any audio with Audio Hijack... Read more
Intermission 1.1.1 - Pause and rewind li...
Intermission allows you to pause and rewind live audio from any application on your Mac. Intermission will buffer up to 3 hours of audio, allowing users to skip through any assortment of audio... Read more
Autopano Giga 3.6 - Stitch multiple imag...
Autopano Giga allows you to stitch 2, 20, or 2,000 images. Version 3.0 integrates impressive new features that will definitely make you adopt Autopano Pro or Autopano Giga: Choose between 9... Read more
Airfoil 4.8.7 - Send audio from any app...
Airfoil allows you to send any audio to AirPort Express units, Apple TVs, and even other Macs and PCs, all in sync! It's your audio - everywhere. With Airfoil you can take audio from any... Read more
Microsoft Remote Desktop 8.0.8 - Connect...
With Microsoft Remote Desktop, you can connect to a remote PC and your work resources from almost anywhere. Experience the power of Windows with RemoteFX in a Remote Desktop client designed to help... Read more
xACT 2.30 - Audio compression toolkit. (...
xACT stands for X Aaudio Compression Toolkit, an application that encodes and decodes FLAC, SHN, Monkey’s Audio, TTA, Wavpack, and Apple Lossless files. It also can encode these formats to MP3, AAC... Read more

Latest Forum Discussions

See All

This Week at 148Apps: July 21-25, 2014
Another Week of Expert App Reviews   At 148Apps, we help you sort through the great ocean of apps to find the ones we think you’ll like and the ones you’ll need. Our top picks become Editor’s Choice, our stamp of approval for apps with that little... | Read more »
Reddme for iPhone - The Reddit Client (...
Reddme for iPhone - The Reddit Client 1.0 Device: iOS iPhone Category: News Price: $.99, Version: 1.0 (iTunes) Description: Reddme for iPhone is an iOS 7-optimized Reddit client that offers a refreshing new way to experience Reddit... | Read more »
Jacob Jones and the Bigfoot Mystery : Ep...
Jacob Jones and the Bigfoot Mystery : Episode 2 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Jacob Jones is back in Episode 2 of one of Apples 'Best of 2013' games and an App Store... | Read more »
New Trailer For Outcast Odyssey, A New K...
New Trailer For Outcast Odyssey, A New Kind of Card Battler Posted by Jennifer Allen on July 25th, 2014 [ permalink ] Out this Fall is a new kind of card battle game: Outcast Odyssey. | Read more »
Hay Day – Tip, Tricks, Strategies, and C...
Recently got into Supercell’s other huge hit, Hay Day and could do with some advice on what to do? We’ve got you covered with some helpful trips and tricks to bear in mind! Ticking Along One of the key things to keep in mind while building up that... | Read more »
Monster Head Review
Monster Head Review By Nadia Oxford on July 25th, 2014 Our Rating: :: FEEDING TIMEUniversal App - Designed for iPhone and iPad Racking up a high score with Monster Head is trickier than it first appears. The appeal wears out fairly... | Read more »
Garfield: Survival of the Fattest Coming...
Garfield: Survival of the Fattest Coming to iOS this Fall Posted by Jennifer Allen on July 25th, 2014 [ permalink ] Who loves lasagna? Me. Also everyone’s favorite grumpy fat cat, Garfield. | Read more »
Happy Flock Review
Happy Flock Review By Andrew Fisher on July 25th, 2014 Our Rating: :: HERD IT ALL BEFOREUniversal App - Designed for iPhone and iPad Underneath the gloss of Happy Flock’s visuals is a game of very little substance. It’s cute, but... | Read more »
Square Register Updates Adds Offline Pay...
Square Register Updates Adds Offline Payments Posted by Ellis Spice on July 25th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Looking For Group – Hearthstone’s Curse...
For the first time since its release (which has thankfully been a much shorter window for iPad players than their PC counterparts), Blizzard’s wildly successful Hearthstone: Heroes of Warcraft CCG is sporting some brand new content: the single... | Read more »

Price Scanner via MacPrices.net

Apple’s 2014 Back to School promotion: $100 g...
 Apple’s 2014 Back to School promotion includes a free $100 App Store Gift Card with the purchase of any new Mac (Mac mini excluded), or a $50 Gift Card with the purchase of an iPad or iPhone,... Read more
iMacs on sale for $150 off MSRP, $250 off for...
Best Buy has iMacs on sale for up to $160 off MSRP for a limited time. Choose free home shipping or free instant local store pickup (if available). Prices are valid for online orders only, in-store... Read more
Mac minis on sale for $100 off MSRP, starting...
Best Buy has Mac minis on sale for $100 off MSRP. Choose free shipping or free instant local store pickup. Prices are for online orders only, in-store prices may vary: 2.5GHz Mac mini: $499.99 2.3GHz... Read more
Global Tablet Market Grows 11% in Q2/14 Notwi...
Worldwide tablet sales grew 11.0 percent year over year in the second quarter of 2014, with shipments reaching 49.3 million units according to preliminary data from the International Data Corporation... Read more
New iPhone 6 Models to Have Staggered Release...
Digitimes’ Cage Chao and Steve Shen report that according to unnamed sources in Apple’s upstream iPhone supply chain, the new 5.5-inch iPhone will be released several months later than the new 4.7-... Read more
New iOS App Helps People Feel Good About thei...
Mobile shoppers looking for big savings at their favorite stores can turn to the Goodshop app, a new iOS app with the latest coupons and deals at more than 5,000 online stores. In addition to being a... Read more
Save on 5th generation refurbished iPod touch...
The Apple Store has Apple Certified Refurbished 5th generation iPod touches available starting at $149. Apple’s one-year warranty is included with each model, and shipping is free. Many, but not all... Read more
What Should Apple’s Next MacBook Priority Be;...
Stabley Times’ Phil Moore says that after expanding its iMac lineup with a new low end model, Apple’s next Mac hardware decision will be how it wants to approach expanding its MacBook lineup as well... Read more
ArtRage For iPhone Painting App Free During C...
ArtRage for iPhone is currently being offered for free (regularly $1.99) during Comic-Con San Diego #SDCC, July 24-27, in celebration of the upcoming ArtRage 4.5 and other 64-bit versions of the... Read more
With The Apple/IBM Alliance, Is The iPad Now...
Almost since the iPad was rolled out in 2010, and especially after Apple made a 128 GB storage configuration available in 2012, there’s been debate over whether the iPad is a serious tool for... Read more

Jobs Board

*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
WW Sales Program Manager, *Apple* Online St...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
Lead Software Engineer, *Apple* Online Stor...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
Manager, *Apple* Fullfillment Operation (AF...
…cross-functional teams to drive the highest level of program management quality for Apple . You will help plan launch strategy and demand generation programs with Read more
Project Manager / Business Analyst, WW *Appl...
…a senior project manager / business analyst to work within our Worldwide Apple Fulfillment Operations and the Business Process Re-engineering team. This role will work Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.