Home > Store

C++ FAQs: Frequently Asked Questions

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

C++ FAQs: Frequently Asked Questions

Book

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

Description

  • Copyright 1995
  • Dimensions: 6-1/4x9-1/4
  • Pages: 496
  • Edition: 1st
  • Book
  • ISBN-10: 0-201-58958-3
  • ISBN-13: 978-0-201-58958-0

Second Edition
now available!
This book is inspired by the popularity of the electronic FAQ, originally prepared and disseminated by Marshall Cline and available on the Internet at comp.lang.c++. The direct question and answer format makes it easy for readers to quickly find the information they are looking for. This book contains four to five times the material of the electronic FAQ: new questions and answers have been added, existing questions have been extensively revised, comprehensive examples illustrate key points and provide practical guidelines for programmers, and thorough cross referencing makes this book a professional guidebook. The authors go beyond simply answering questions, they present a clear philosophy that promotes high quality C++ programming. The authors combine a lively, straightforward style with just enough humor to make the book accessible to the beginning C++ programmer, and a valuable reference for the experienced C++ developer.

0201589583B04062001

Downloads

Example(s)

Click below for Example(s) related to this title:
examples.exe

examples.sit.hqx

examples.tar.Z

Sample Content

Table of Contents



Introduction.


Fundamentals of Object-Oriented Technology.


Fundamentals of Extensibility.


Fundamentals of C++.


Designing Classes and Objects.


Designing Good Interfaces.


Fundamentals of Inheritance.


Dynamic Binding.


Specifying Observable Behavior.


Proper Inheritance.


Correcting Improper Inheritance.


Edge-Effects of Inheritance.


Static and Dynamic Typing.


Constructors and Destructors.


Initialization Lists.


The Big Three.


User-Defined Assignment Operators.


Designing for Testability.


Friend Classes and Friend Functions.


Strategic Advice for Exception Handling.


Tactical Advice for Exception Handling.


Templates.


References.


New and Delete.


Preventing Memory Leaks.


Preventing Wild Pointers.


Const Correctness.


Operator Overloading.


Exploring Inline Functions.


Reference and Value Semantics.


Performance Tuning.


Input/Output.


Static Class Members.


Inter-Language Linkage with C.


Training.


Coding Standards.


Leaf Classes.


C++ and Smalltalk.


Private and Protected Inheritance.


Pointers to Member Functions.


Source Code.


Miscellaneous.


Getting More Information on C++. 0201589583T04062001

Preface

FAQ 0.1

What is the purpose of this book?

The purpose of the book is to change the way you think.

Changing the way you think is much more challenging than learning the syntax of a programming language. For example, learning when and why you should use a particular construct is much harder than learning the syntax for that construct. Learning when not to use a particular construct is even harder.

This book gives specific advice and directions to help you properly use C++ for object-oriented programming. If you are new to object-oriented programming, our aim is to convert you to the object-oriented way of thinking. If you are a seasoned veteran, our aim is to have you question some of your ingrained practices and possibly adopt new approaches to using C++ --approaches that will scale better.

FAQ 0.2

What is unique about the style and format of this book?

To achieve the book's goals, we used a novel style and format. Here are the key elements of that style and format.

  • A question and answer format: This book contains answers to Frequently Asked Questions (or FAQs) about using C++ for object-oriented programming. These FAQs are the product of several years of teaching others to use C++ effectively, as well as an embarrassingly large number of hours corresponding with the international C++ user community via the Internet news group, comp.lang.c++.
  • A focus on principles and concepts rather than syntax: Rather than focusing on the syntax and semantics of various C++ language features, this book shows how to combine features properly and why to combine them.
  • A consistent programming and design philosophy: As with any programming language, there are numerous ways to use and combine the various features of C++. Some of these uses and combinations lead to comprehendable, maintainable, extensive, and reusable software; other uses and combinations only work in small examples and are, in reality, abuses of the language. These FAQs identify which uses and combinations are preferred and promote a consistent programming and design philosophy that has good scaling properties.
  • Lots of programming examples: This book contains almost 200 programming examples, most of which are complete, runnable programs rather than program fragments. Readers are encouraged to dissect and execute the examples because this will enhance the learning experience.
  • Extensive cross referencing: The FAQs in this book are extensively cross referenced to other FAQs, as well as to other C++ books. Since the topic of using C++ for object-oriented programming is too large to be completely covered by one book, we provide you with pointers into other books where complementary material can be found. Three books we selected for cross referencing: Stroustrup's The C++ Programming Language, Second Edition, an excellent description of the language; Ellis and Stroustrup's The Annotated C++ Reference Manual, an authoritative and comprehensive definition of the language; and Lippman's C++ Primer, Second Edition, an excellent introductory book.
  • A lighthearted style: We want to change the way you think, so we have to debunk many common assumptions. This leads to the questioning of common practices and --to take the edge off-- humor. Another reason for the lighthearted style is that this book has roots in comp.lang.c++, where the communication style is direct and unforgiving, and where humor is used to calm people's passions and cajole people into seeing your point of view :-)

FAQ 0.3

Where did these FAQs come from?

We made them up.

Surprisingly, most of the FAQs contained in this book are not questions that anyone ever asked us directly. Instead they come from our experience training developers in object-oriented technology and from corresponding with readers on comp.lang.c++. Topics that come up again and again, whether during training sessions or on comp.lang.c++, were coalesced into a series of questions.

FAQ 0.4

Is every topic treated with equal emphasis?

No.

Some topics are covered in greater depth than other topics. We focus more energy on topics that historically have been most difficult for people to get right, and we almost completely ignore placement of semicolons and other issues that the compiler will tell you about. The more subtle the problem, the more we focus on it --especially practices that are accepted by the compiler and appear to function correctly, but which increase the overall cost of the software.

FAQ 0.5

How should you use this book in combination with other books?

It depends on what you're trying to achieve.

This book is not intended to be a tutorial introduction to C++. Each example is meant to illustrate a single idea relevant to that FAQ. Many introductory aspects of the language, such as the syntax of a for loop, are assumed. If you want to cover the fundamentals of C++, we recommend using this book in conjunction with either The C++ Programming Language, Second Edition or C++ Primer, Second Edition. Our FAQs provide extensive cross references into both of these texts.

This book is not intended to be a reference manual for C++. It does not pretend to cover all aspects of the language in encyclopedic detail. If you want such a reference manual, we recommend using this book in conjunction with The Annotated C++ Reference Manual. Our FAQs provide extensive cross references into this text.

FAQ 0.6

How is the book organized?

The FAQs are organized into more than 40 chapters, each of which is organized into ascending complexity.

The FAQs in each chapter all deal with a specific topic. Usually the questions near the beginning of a chapter deal with basic questions and fundamental concepts; later questions deal with advanced issues and complex topics.

The first third of the book deals with object-oriented aspects of C++ such as inheritance, dynamic binding, and polymorphism. The middle portion of the book deals with a variety of C++ facilities ranging from templates to exception handling to the proper management of pointers. The last portion of the book contains chapters that deal with environmental topics such as training, coding standards, and Smalltalk.

FAQ 0.7

Why did we create the electronic FAQ?

To bring order to chaos.

First, the electronic FAQ presents the facts (pun intended) about C++ in a more concise and focused manner than the somewhat chaotic discussions on comp.lang.c++. Second, the number of developers switching to C++ is amazing, and the electronic FAQ answers many basic questions for these new users. Third, without a FAQ list, seasoned veterans would become irritated by answering the same questions over and over; the electronic FAQ encourages the veterans to act more like ambassadors. In the end, the electronic FAQ makes comp.lang.c++ a kinder, gentler news group, where people are less likely to be "flamed" (a technical term for posting a particularly scathing, blistering, and sarcastic response to something that is posted to an electronic bulletin board).

FAQ 0.8

Why did we write this book?

To further spread the good news.

We decided to write this book for several reasons. First, Addison-Wesley expressed interest in such a project. Second, it would make the FAQs available to a wider audience since not everyone has access to Internet and comp.lang.c++. Third, a book permits the material to be presented in a more professional manner than can be easily done with the electronic FAQ. Fourth this project spurred us to expand the material vastly beyond what is provided by the electronic FAQ. In the end, after adding all the new FAQs, examples, and internal and external cross references, the book contains five times more material than the electronic FAQ.

FAQ 0.9

Are Marshall's and Greg's motivations based on language bigotry?

No.

For the record, we are not now --nor have we ever been-- C++ language bigots. We love Smalltalk, Eiffel, C, Lisp, CLOS, Prolog, Simula, Ada, Modula, Pascal, perl, awk, sed, csh, ksh, REXX, COBOL, FORTRAN, RPG, etc. At the same time, we adopted the philosophy that since we're writing a book about C++, we might as well throw ourselves into the project even if it means that we look like C++ fanatics. It certainly has made for more exciting and provocative writing and, we hope, reading.

Our main goal is to get you to think about what you are doing and how you might improve on it. If we accomplish this, then we will be satisfied.

FAQ 0.10

How can you communicate with us?

Email and FTP.

We'd love to hear from you. Our email address is clinefaq@aw.com. Send us your questions. Send us your comments.

You can acquire a free, machine-readable copy of the code sections in this book via FTP (ask your favorite network guru for help on how to access FTP with your software). The code sections will be available via FTP aw.com in the directory aw.computer.science/clinefaq. Use anonymous as your user ID, and use your email address as your password.

Enjoy!



0201589583P04062001

Updates

Errata

Errata for "C++ Frequently Asked Questions"
Marshall Cline/Greg Lomow
0-201-58958-3

Table of contents (pg vi): title of FAQ 50:
old: ... help beat the speed/safety tradeoff?
new: ... help beat the performance/encapsulation tradeoff?

Table of contents (pg xv): title of FAQ 316:
old: ... an object to manage a resource and also perform ...
new: ... a resource manager object's constructor to perform ...

Acknowledgments (pg xxi):
old: Steve Clammage
new: Steve Clamage

FAQ 10 (page 5): Add after the sentence ending "have an explicit return type.":
add: Also, we exploit the new C++ feature that requires the compiler to insert an implicit return 0; at 

end of main().

FAQ 88 (pg 66):
old: virtual nStack() { }
new: virtual ~Stack() { }

FAQ 117
old: Substitutablility
new: Substitutability

FAQ 141 (pg 128): Program output:
old: Derived::g(float)
 Derived::g(float)
new: Base::g(float)
 Base::g(float)

FAQ 142 (pg 129): Program output:
old: Derived::f(float)
 Derived::f(float)
new: Base::f(float)
 Base::f(float)

FAQ 180 (pg 171): Add at the very end of the answer:
add: If you use this technique, do not access the static object from the destructor of another static 

object.

FAQ 206 (pg 203):
old: systhesized
new: synthesized

FAQ 226 (pg 229): remove the line:  --numElems_;

FAQ 227 (pg 230):
old: ... must re-verify the object's state and its transitions there ...
new: ... must continually re-verify the object's state and its transitions, but there ...

FAQ 228 (pg 231): "friend DataBase;" should be private in class DataBaseCursor:
old: protected:
new: private:

FAQ 240 (pg 243):
old: friend Fraction operator*(const Fraction&,const Fraction&)
new: friend Fraction operator*(const Fraction& a,const Fraction& b)

FAQ 240 (pg 243): change "ostr" to "o" (in BOTTOM example on this page):
old: ostr << f.num_
new: o << f.num_

FAQ 245: contents of pgs 248 and 249 are swapped.

FAQ 265 (pg 270):
old: ...living dead -- object's that aren't quite alive but aren't quite...
new: ...living dead -- an object that isn't quite alive but isn't quite...

FAQ 266 (pg 272):
old: A() throw(int)  : x(), y() { cout << "A ctor\n"; }
new: A() throw(int)  : x_(), y_() { cout << "A ctor\n"; }

FAQ 271 (pg 276):
old: #include 
new: #include   //or  ...

FAQ 272 (pg 278): first line:
old: The unexpected function...
new: The unexpected() function...

FAQ 282 (pg 288):
old: //BAD FORM: manipulates (i.e., "manipulates") the bits of
 //the T objects:
new: //BAD FORM: manipulates the bits of the T objects:

FAQ 282 (pg 288):
old: #if 1s
new: #if 1

FAQ 302 (pg 304): In the "Flexibility:" paragraph
old: overridden
new: overloaded

FAQ 311 (pg 310):
old: protected:
new: private:

FAQ 313 (pg 312):
old: unsigned chk(unsigned i)          //checks index i
new: unsigned chk(unsigned i) const    //checks index i

FAQ 313 (pg 312): Change "T" to "int":
old: T*       arr_;
new: int*     arr_;

FAQ 342 (pg 340):
old: operator* (Number a, Number a)
new: operator* (Number a, Number b)

FAQ 380 (pg 376): In default ctor init list:
old: serial_(num)
new: serial_(num_)

FAQ 425 (pg 408): Paragraph that starts "Member function names":
old: ...class names are introduced with...
new: ...member function names are introduced with...

FAQ 425 (pg 409): Paragraph that starts "Data member names":
old: ...in multi-word class names...
new: ...in multi-word data member names...

FAQ 456 (pg 432):
old: #include 
new: #include 

Index (pg 453): Under "Evil things:"
old: evaluating inheritance via "specification"
new: evaluating inheritance via "specialization"



---
Marshall P. Cline, Ph.D.
President, Paradigm Shift, Inc.
P.O. Box 5108 / Potsdam, NY  13676
cline@parashift.com / 315-353-6100(voice) / 315-353-6110(fax)

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