 |
C++ FAQ's, 2nd Edition Customer Reviews: 1 Average Customer Rating:      Write a Review and tell the world about this title! People who purchase this book frequently purchase: - Exceptional C++ Style: 40 New Engineering Puzzles, Programming Problems, and Solutions; Herb Sutter, $34.95, 22% Off!
- Modern C++ Design; Andrei Alexandrescu, $46.50, 22% Off!
- More Exceptional C++: 40 Engineering Puzzles, Programming Problems, and Solutions; Herb Sutter, $34.95, 22% Off!
- C++ Coding Standards: 101 Rules, Guidelines, and Best Practices; Herb Sutter, et al, $34.95, 22% Off!
Books on similar topics, in best-seller order:Books from the same publisher, in best-seller order:
In a concise and direct question-and-answer format, C++ FAQs, Second Edition brings you the most efficient solutions to more than four hundred of the practical programming challenges you face every day.
Moderators of the on-line C++ FAQ at comp.lang.c++, Marshall Cline, Greg Lomow, and Mike Girou are familiar with C++ programmers' most pressing concerns. In this book, the authors concentrate on those issues most critical to the professional programmer's work, and they present more explanatory material and examples than is possible on-line. This book focuses on the effective use of C++, helping programmers avoid combining seemingly legal C++ constructs in incompatible ways.
This second edition is completely up-to-date with the final ANSI/ISO C++ Standard. It covers some of the smaller syntax changes, such as "mutable"; more significant changes, such as RTTI and namespaces; and such major innovations as the C++ Standard Library, including the STL. In addition, this book discusses technologies such as Java, CORBA, COM/COM+, and ActiveX--and the relationship all of these have with C++. These new features and technologies are iconed to help you quickly find what is new and different in this edition.
Each question-and-answer section contains an overview of the problem and solution, fuller explanations of concepts, directions for proper use of language features, guidelines for best practices and practices to avoid, and plenty of working, stand-alone examples. This edition is thoroughly cross-referenced and indexed for quick access.
Part I Preliminaries 1
Chapter 1 Introduction 3
FAQ 1.01: What is the purpose of this chapter? 3
FAQ 1.02: What are C++ FAQs? 3
FAQ 1.03: Who is the target audience for this book? 4
FAQ 1.04: Is this a book about C++ per se? 4
FAQ 1.05: Why do developers need a guidebook for C++ and OO technology?
5
FAQ 1.06: What kind of guidance is given in the answers to these FAQs? 5
FAQ 1.07: What is the electronic FAQ and why buy this book when the electronic
FAQ is free? 6
FAQ 1.08: Why should you buy this edition if you already have a copy of
the first edition? 7
FAQ 1.09: What conventions are used in this book? 7
Chapter 2 Basic C++ Syntax and Semantics 11
FAQ 2.01: What is the purpose of this chapter? 11
FAQ 2.02: What are the basics of main()? 11
FAQ 2.03: What are the basics of functions? 12
FAQ 2.04: What are the basics of default parameters? 13
FAQ 2.05: What are the basics of local (auto) objects? 13
FAQ 2.06: What are the basics of constructing objects using explicit parameters?
15
FAQ 2.07: What are the basics of dynamically allocated (new) objects? 16
FAQ 2.08: What are the basics of local objects within inner scopes? 17
FAQ 2.09: What are the basics of passing objects by reference? 18
FAQ 2.10: What are the basics of passing objects by value? 19
FAQ 2.11: What are the basics of passing objects by pointer? 20
FAQ 2.12: What are the basics of stream output? 21
FAQ 2.13: What are the basics of stream input? 22
FAQ 2.14: What are the basics of using classes that contain overloaded operators?
24
FAQ 2.15: What are the basics of using container classes? 24
FAQ 2.16: What are the basics of creating class header files? 26
FAQ 2.17: What are the basics of defining a class? 27
FAQ 2.18: What are the basics of defining member functions? 28
FAQ 2.19: What are the basics of adding a constructor to a class? 29
FAQ 2.20: What are the basics of adding a destructor to a class? 31
FAQ 2.21: What are the basics of defining a class that contains a pointer
to an object allocated from the heap? 32
FAQ 2.22: What are the basics of global objects? 35
FAQ 2.23: What are the basics of throwing and catching exceptions? 37
FAQ 2.24: What are the basics of inheritance and dynamic binding? 39
Chapter 3 Understanding the Management Perspective 43
FAQ 3.01: What is the purpose of this chapter? 43
FAQ 3.02: What is the core message of this chapter (and this book)? 44
FAQ 3.03: Why are the managers in charge rather than the developers who
understand technology? 45
FAQ 3.04: How can someone manage something they don't understand? 45
FAQ 3.05: What is the most common mistake on C++ and OO projects? 46
FAQ 3.06: What's the "Software Peter Principle"? 47
FAQ 3.07: Should an organization use OO on all its projects? 47
FAQ 3.08: Can OO be ignored until it goes away? 48
FAQ 3.09: What OO language is best? 48
FAQ 3.10: What is the right approach to processes and tools? 49
FAQ 3.11: What is the right approach with off-the-shelf class libraries
and frameworks? 50
Chapter 4 The Architectural Perspective 51
FAQ 4.01: What is the purpose of this chapter? 51
FAQ 4.02: Why is software architecture important? 52
FAQ 4.03: What should the architecture be based on, the problem being solved
or the problem domain? 53
FAQ 4.04: Should the software architecture be based on the policy of the
problem? 53
FAQ 4.05: Do customers ever change their requirements? 54
FAQ 4.06: Are stable requirements desirable? 54
FAQ 4.07: What is the key to planning for change? 55
FAQ 4.08: What is a framework? 55
FAQ 4.09: What is the "inversion of control" exhibited by frameworks? 56
FAQ 4.10: What is an extensible, domain-specific framework? 56
FAQ 4.11: What characteristics make a framework extensible yet domain-specific?
57
FAQ 4.12: What happens if the domain analysis is incorrect? 58
FAQ 4.13: How much effort should be expended to support change--that is,
how much is extensibility worth? 58
FAQ 4.14: How does an architect make the software architecture flexible?
59
FAQ 4.15: What is the secret to achieving reuse? 59
Part II Object-Oriented Design 61
Chapter 5 Object-Oriented Fundamentals 63
FAQ 5.01: What is the purpose of this chapter? 63
FAQ 5.02: Why is the world adopting OO technology? 63
FAQ 5.03: What are some of the benefits of using C++ for OO programming?
64
FAQ 5.04: What are the fundamental concepts of object-oriented technology?
65
FAQ 5.05: Why are classes important? 66
FAQ 5.06: What is an object? 66
FAQ 5.07: What are the desirable qualities of an object? 67
FAQ 5.08: How are classes better than the three basic building blocks of
procedural software? 68
FAQ 5.09: What is the purpose of composition? 69
FAQ 5.10: What is the purpose of inheritance? 70
FAQ 5.11: What are the advantages of polymorphism and dynamic binding? 71
FAQ 5.12: How does OO help produce flexible and extensible software? 71
FAQ 5.13: How can old code call new code? 72
FAQ 5.14: What is an abstraction and why is it important? 75
FAQ 5.15: Should abstractions be user-centric or developer-centric? 75
FAQ 5.16: What's the difference between encapsulation and abstraction? 76
FAQ 5.17: What are the consequences of encapsulating a bad abstraction?
76
FAQ 5.18: What's the value of separating interface from implementation?
77
FAQ 5.19: How can separating interface from implementation improve performance
as well as flexibility? 78
FAQ 5.20: What is the best way to create a good interface to an abstraction?
79
FAQ 5.21: How are get/set member functions related to poorly designed interfaces?
80
FAQ 5.22: Should there be a get and a set member function for each member
datum? 80
FAQ 5.23: Is the real purpose of a class to export data? 81
FAQ 5.24: Should OO be viewed as data-centric? 81
Chapter 6 Specification of Observable Behavior 83
FAQ 6.01: What is the purpose of this chapter? 83
FAQ 6.02: Should users of a member function rely on what the code actually
does or on the specification? 84
FAQ 6.03: What are the advantages of relying on the specification rather
than the implementation? 85
FAQ 6.04: What are advertised requirements and advertised promises? 86
FAQ 6.05: How are the advertised requirements and advertised promises of
the member functions specified? 87
FAQ 6.06: Why are changes feared in development organizations that don't
use specification? 89
FAQ 6.07: How do developers determine if a proposed change will break existing
code? 90
FAQ 6.08: What are the properties of a substitutable (backward compatible)
change in a specification? 90
FAQ 6.09: How can it be shown that the implementation of a member function
fulfills its specification? 91
FAQ 6.10: Is it possible to keep the specification synchronized with the
code? 92
Chapter 7 Proper Inheritance 95
FAQ 7.01: What is proper inheritance? 95
FAQ 7.02: What are the benefits of proper inheritance? 97
FAQ 7.03: What is improper inheritance? 97
FAQ 7.04: Isn't the difference between proper and improper inheritance obvious?
98
FAQ 7.05: Is substitutability based on what the code does or what the specification
promises the code will do? 99
FAQ 7.06: Is it proper to revoke (hide) an inherited public: member function?
100
FAQ 7.07: What is specialization? 101
FAQ 7.08: What do subsets have to do with proper inheritance? 102
Chapter 8 Detecting and Correcting Improper Inheritance 103
FAQ 8.01: Can improper inheritance wreck a project? 103
FAQ 8.02: What's the best way to learn how to avoid improper inheritance?
104
FAQ 8.03: Is intuition a reliable guide to understanding proper inheritance?
104
FAQ 8.04: Is an Ostrich a kind-of Bird? 104
FAQ 8.05: Should an overridden virtual function throw an exception? 106
FAQ 8.06: Can an overridden virtual function be a no-op? 109
FAQ 8.07: Why does C++ make it so hard to fix the Ostrich/Bird dilemma?
111
FAQ 8.08: Should Circle inherit from Ellipse? 111
FAQ 8.09: What can be done about the asymmetric-circle dilemma? 113
FAQ 8.10: What is the one issue in these FAQs that doesn't seem to die?
115
FAQ 8.11: Should Stack inherit from List? 115
FAQ 8.12: Is code reuse the main purpose of inheritance? 117
FAQ 8.13: Is container-of-thing a kind-of container-of-anything? 119
FAQ 8.14: Is bag-of-apple a kind-of bag-of-fruit, assuming bag-of-fruit
allows the insertion of any kind-of fruit? 119
FAQ 8.15: Is parking-lot-for-cars a kind-of parking-lot-for-arbitrary-vehicles
(assuming parking-lot-for-vehicles allows parking any kind-of vehicle)?
123
FAQ 8.16: Is array-of Derived a kind-of array-of Base? 124
FAQ 8.17: Does the fact that an array-of Derived can be passed as an array-of
Base mean that arrays are bad? 126
Chapter 9 Error Handling Strategies 129
FAQ 9.01: Is error handling a major source of fundamental mistakes? 129
FAQ 9.02: How should runtime errors be handled in C++? 130
FAQ 9.03: What happens to objects in stack frames that become unwound during
the throw / catch process? 131
FAQ 9.04: What is an exception specification? 131
FAQ 9.05: What are the disadvantages of using return codes for error handling?
132
FAQ 9.06: What are the advantages of throw...catch? 132
FAQ 9.07: Why is it helpful to separate normal logic from exception handling
logic? 133
FAQ 9.08: What is the hardest part of using exception handling? 136
FAQ 9.09: When should a function throw an exception? 137
FAQ 9.10: What is the best approach for the hierarchy of exception objects?
140
FAQ 9.11: How should exception classes be named? 141
FAQ 9.12: Where do setjmp and longjmp belong in
C++? 141
Chapter 10 Testing Strategies 143
FAQ 10.01: What is the purpose of this chapter? 143
FAQ 10.02: What are the advantages of self-testing objects? 143
FAQ 10.03: What are some common excuses people use for not building self-testing
into their objects? 144
FAQ 10.04: What will happen if techniques like those presented here are
not used? 145
FAQ 10.05: When is a class correct? 146
FAQ 10.06: What is behavioral self-testing? 146
FAQ 10.07: What is a class invariant? 149
FAQ 10.08: Why should the invariant be captured explicitly? 149
FAQ 10.09: When should the testInvariant() member function be called? 150
FAQ 10.10: What can be done to ensure that an object doesn't get blown away
by a wild pointer? 151
Part III Language Facilities 153
Chapter 11 References 155
FAQ 11.01: What is a reference? 155
FAQ 11.02: What does "referent" mean? 156
FAQ 11.03: When can a reference be attached to its referent? 156
FAQ 11.04: What happens when a value is assigned to a reference? 157
FAQ 11.05: What is a local reference? 157
FAQ 11.06: What does it mean to return a reference? 158
FAQ 11.07: What is the result of taking the address of a reference? 159
FAQ 11.08: Can a reference be made to refer to a different referent? 160
FAQ 11.09: Why use references when pointers can do everything references
can do? 160
FAQ 11.10: Aren't references just pointers in disguise? 161
FAQ 11.11: When are pointers needed? 161
FAQ 11.12: Why do some people hate references? 162
FAQ 11.13: Does int& const x make sense? 163
Chapter 12 New and Delete 165
FAQ 12.01: Does new do more than allocate memory? 165
FAQ 12.02: Why is new better than good old trustworthy malloc()?
165
FAQ 12.03: Does C++ have a counterpart to realloc() that goes
along with new and delete? 166
FAQ 12.04: Can pointers returned from new be deallocated with
free()? Can pointers returned from malloc() be
deallocated with delete? 166
FAQ 12.05: Does delete p delete the pointer p
or the referent *p? 167
FAQ 12.06: Should the pointer returned from new Fred() be checked
to see if it is NULL? 167
FAQ 12.07: How can new be convinced to return NULL rather than
throw an exception? 168
FAQ 12.08: How can new be set up to automatically flush pools
of recycled objects whenever memory runs low? 169
FAQ 12.09: What happens if delete p is called when p
is NULL? 172
FAQ 12.10: What happens when a pointer is deleted twice? 172
FAQ 12.11: How can an array of things be allocated and deallocated? 173
FAQ 12.12: What if delete p (not delete[] p) is
used to delete an array allocated via new Fred[n]? 174
FAQ 12.13: Can the [] of delete[] p be dropped
when p points to an array of some built-in type such as char?
174
FAQ 12.14: How is an object constructed at a predetermined position in memory?
175
FAQ 12.15: How can class Fred guarantee that Fred
objects are created only with new and not on the stack? 176
FAQ 12.16: How are objects created by placement new destroyed?
177
FAQ 12.17: In p = new Fred(), does the Fred memory
"leak" if the Fred constructor throws an exception? 178
FAQ 12.18: Is it legal (and moral) for a member function to say delete
this? 179
FAQ 12.19: After p = new Fred[n], how does the compiler know
that there are n objects to be destructed during delete[]
p? 180
Chapter 13 Inline Functions 183
FAQ 13.01: What is the purpose of inline functions? 183
FAQ 13.02: What is the connection between the keyword "inline" and "inlined"
functions? 184
FAQ 13.03: Are there any special rules about inlining? 184
FAQ 13.04: What is the one-definition rule (ODR)? 185
FAQ 13.05: What are some performance considerations with inline functions?
185
FAQ 13.06: Do inlined functions improve performance? 186
FAQ 13.07: Do inlined functions increase the size of the executable code?
187
FAQ 13.08: Why shouldn't the inlining decision be made when the code is
first written? 188
FAQ 13.09: What happens when a programmer uses an inlined function obtained
from a third party? 189
FAQ 13.10: Is there an easy way to swap between inline and non-inline code?
190
Chapter 14 Const Correctness 193
FAQ 14.01: How should pointer declarations be read? 193
FAQ 14.02: How can C++ programmers avoid making unexpected changes to objects?
194
FAQ 14.03: Does const imply runtime overhead? 195
FAQ 14.04: Does const allow the compiler to generate more efficient
code? 195
FAQ 14.05: Is const correctness tedious? 196
FAQ 14.06: Why should const correctness be done sooner rather
than later? 196
FAQ 14.07: What's the difference between an inspector and a mutator? 197
FAQ 14.08: When should a member function be declared as const?
198
FAQ 14.09: Does const apply to the object's bitwise state or
its abstract state? 199
FAQ 14.10: When should const not be used in declaring formal
parameters? 200
FAQ 14.11: When should const not be used in declaring a function
return type? 201
FAQ 14.12: How can a "nonobservable" data member be updated within a const
member function? 202
FAQ 14.13: Can an object legally be changed even though there is a const
reference (pointer) to it? 203
FAQ 14.14: Does const_cast mean lost optimization opportunities?
204
Chapter 15 Namespaces 207
FAQ 15.01: What is the purpose of this chapter? 207
FAQ 15.02: What is a namespace? 208
FAQ 15.03: How can code outside a namespace use names declared within that
namespace? 209
FAQ 15.04: What happens if two namespaces contain the same name? 210
FAQ 15.05: What are some of the rules for using namespaces? 211
FAQ 15.06: What is name lookup? 212
FAQ 15.07: What are the tradeoffs between the various techniques
for using names from a namespace, particularly the standard namespace? 213
FAQ 15.08: Can namespaces break code? 213
FAQ 15.09: Do namespaces have any other applications? 215
FAQ 15.10: How do namespaces solve the problem of long identifiers? 215
Chapter 16 Using Static 217
FAQ 16.01: What is the purpose of this chapter? 217
FAQ 16.02: What are static class members? 217
FAQ 16.03: What is an analogy for static data members? 218
FAQ 16.04: Can inline functions safely access static data members? 220
FAQ 16.05: What is an analogy for static member functions? 222
FAQ 16.06: How is a static data member similar to a global variable? 224
FAQ 16.07: How is a static member function similar to a friend function?
224
FAQ 16.08: What is the named constructor idiom? 225
FAQ 16.09: How should static member functions be called? 226
FAQ 16.10: Why might a class with static data members get linker errors?
227
FAQ 16.11: How is a const static data member initialized? 227
FAQ 16.12: What is the right strategy for implementing a function that needs
to maintain state between calls? 229
FAQ 16.13: How can the function call operator help with functionoids? 230
FAQ 16.14: Is it safe to be ignorant of the static initialization order
problem? 231
FAQ 16.15: What is a simple and robust solution to the static initialization
order problem? 233
FAQ 16.16: What if the static object's destructor has important side effects
that must eventually occur? 234
FAQ 16.17: What if the static object's destructor has important side effects
that must eventually occur and the static object must be accessed by another
static object's destructor? 235
FAQ 16.18: What are some criteria for choosing between all these various
techniques? 237
Chapter 17 Derived Classes 239
FAQ 17.01: What is the purpose of this chapter? 239
FAQ 17.02: How does C++ express inheritance? 239
FAQ 17.03: What is a concrete derived class? 241
FAQ 17.04: Why can't a derived class access the private: members
of its base class? 242
FAQ 17.05: How can a base class protect derived classes so that changes
to the base class will not affect them? 243
FAQ 17.06: Can a derived class pointer be converted into a pointer to its
public base class? 245
FAQ 17.07: How can a class Y be a kind-of another class X as well as getting
the bits of X? 245
FAQ 17.08: How can a class Y get the bits of an existing class X without
making Y a kind-of X? 246
FAQ 17.09: How can a class Y be a kind-of another class X without inheriting
the bits of X? 247
Chapter 18 Access Control 249
FAQ 18.01: What is the purpose of this chapter? 249
FAQ 18.02: How are private:, protected:, and public:
different? 249
FAQ 18.03: Why can't subclasses access the private: parts of
their base class? 250
FAQ 18.04: What's the difference between the keywords struct
and class? 250
FAQ 18.05: When should a data member be protected: rather than
private:? 251
FAQ 18.06: Why is private: the default access level for a class?
251
Chapter 19 Friend Classes and Friend Functions 253
FAQ 19.01: What is a friend? 253
FAQ 19.02: What's a good mental model for friend classes? 255
FAQ 19.03: What are some advantages of using friend classes? 255
FAQ 19.04: Do friends violate the encapsulation barrier? 256
FAQ 19.05: What is a friend function? 256
FAQ 19.06: When should a function be implemented as a friend function rather
than a member function? 257
FAQ 19.07: What are some guidelines to make sure friend functions are used
properly? 258
FAQ 19.08: What does it mean that friendship isn't transitive? 258
FAQ 19.09: What does it mean that friendship isn't inherited? 260
FAQ 19.10: What does it mean that friends aren't virtual? 261
FAQ 19.11: What qualities suggest a friend function rather than a member
function? 263
FAQ 19.12: Should friend functions be declared in the private:,
protected:, or public: section of a class? 265
FAQ 19.13: What is a private class? 266
FAQ 19.14: How are objects of a class printed? 267
FAQ 19.15: How do objects of a class receive stream input? 268
Chapter 20 Constructors and Destructors 269
FAQ 20.01: What is the purpose of a constructor? 269
FAQ 20.02: What is C++'s constructor discipline? 270
FAQ 20.03: What is the purpose of a destructor? 271
FAQ 20.04: What is C++'s destructor discipline? 271
FAQ 20.05: What happens when a destructor is executed? 272
FAQ 20.06: What is the purpose of a copy constructor? 273
FAQ 20.07: When is a copy constructor invoked? 274
FAQ 20.08: What is the "default constructor"? 275
FAQ 20.09: Should one constructor call another constructor as a primitive?
277
FAQ 20.10: Does the destructor for a derived class need to explicitly call
the destructor of its base class? 278
FAQ 20.11: How can a local object be destructed before the end of its function?
278
FAQ 20.12: What is a good way to provide intuitive, multiple constructors
for a class? 281
FAQ 20.13: When the constructor of a base class calls a virtual function,
why isn't the override called? 283
FAQ 20.14: When a base class destructor calls a virtual function, why isn't
the override called? 285
FAQ 20.15: What is the purpose of placement new? 285
Chapter 21 Virtual Functions 287
FAQ 21.01: What is the purpose of this chapter? 287
FAQ 21.02: What is a virtual member function? 287
FAQ 21.03: How much does it cost to call a virtual function compared to
calling a normal function? 288
FAQ 21.04: How does C++ perform static typing while supporting dynamic binding?
288
FAQ 21.05: Can destructors be virtual? 289
FAQ 21.06: What is the purpose of a virtual destructor? 289
FAQ 21.07: What is a virtual constructor? 291
FAQ 21.08: What syntax should be used when a constructor or destructor calls
a virtual function in its object? 293
FAQ 21.09: Should the scope operator :: be used when invoking
virtual member functions? 295
FAQ 21.10: What is a pure virtual member function? 295
FAQ 21.11: Can a pure virtual function be defined in the same class that
declares it? 296
FAQ 21.12: How should a virtual destructor be defined when it has no code?
297
FAQ 21.13: Can an ABC have a pure virtual destructor? 298
FAQ 21.14: How can the compiler be kept from generating duplicate out-lined
copies of inline virtual functions? 299
FAQ 21.15: Should a class with virtual functions have at least one non-inline
virtual function? 299
Chapter 22 Initialization Lists 301
FAQ 22.01: What are constructor initialization lists? 301
FAQ 22.02: What will happen if constructor initialization lists are not
used? 302
FAQ 22.03: What's the guideline for using initialization lists in constructor
definitions? 303
FAQ 22.04: Is it normal for constructors to have nothing inside their body?
303
FAQ 22.05: How is a const data member initialized? 305
FAQ 22.06: How is a reference data member initialized? 306
FAQ 22.07: Are initializers executed in the same order in which they appear
in the initialization list? 306
FAQ 22.08: How should initializers be ordered in a constructor's initialization
list? 308
FAQ 22.09: Is it moral for one member object to be initialized using another
member object in the constructor's initialization list? 309
FAQ 22.10: What if one member object has to be initialized using another
member object? 311
FAQ 22.11: Are there exceptions to the rule "Initialize all member objects
in an initialization list"? 312
FAQ 22.12: How can an array of objects be initialized with specific initializers?
313
Chapter 23 Operator Overloading 315
FAQ 23.01: Are overloaded operators like normal functions? 315
FAQ 23.02: When should operator overloading be used? 316
FAQ 23.03: What operators can't be overloaded? 317
FAQ 23.04: Is the goal of operator overloading to make the class easier
to understand? 318
FAQ 23.05: Why do subscript operators usually come in pairs? 319
FAQ 23.06: What is the most important consideration for operators such as
+=, +, and =? 320
FAQ 23.07: How are the prefix and postfix versions of operator++
distinguished? 321
FAQ 23.08: What should the prefix and postfix versions of operator++
return? 322
FAQ 23.09: How can a Matrix-like class have a subscript operator that takes
more than one subscript? 323
FAQ 23.10: Can a ** operator serve as an exponentiation operator?
325
Chapter 24 Assignment Operators 327
FAQ 24.01: What should assignment operators return? 327
FAQ 24.02: What is wrong with an object being assigned to itself? 328
FAQ 24.03: What should be done about self-assignment? 329
FAQ 24.04: Should an assignment operator throw an exception after partially
assigning an object? 330
FAQ 24.05: How should the assignment operator be declared in an ABC? 332
FAQ 24.06: When should a user-defined assignment operator mimic the assignment
operator that the compiler would generate automatically? 334
FAQ 24.07: What should be returned by private: and protected:
assignment operators? 334
FAQ 24.08: Are there techniques that increase the likelihood that the compiler-synthesized
assignment operator will be right? 335
FAQ 24.09: How should the assignment operator in a derived class behave?
335
FAQ 24.10: Can an ABC's assignment operator be virtual? 337
FAQ 24.11: What should a derived class do a base class's assignment operator
is virtual? 339
FAQ 24.12: Should the assignment operator be implemented by using placement
new and the copy constructor? 341
Chapter 25 Templates 343
FAQ 25.01: What is the purpose of templates? 343
FAQ 25.02: What are the syntax and semantics for a class template? 344
FAQ 25.03: How can a template class be specialized to handle special cases?
346
FAQ 25.04: What are the syntax and semantics for a function template? 348
FAQ 25.05: Should a template use memcpy() to copy objects of
its template argument? 349
FAQ 25.06: Why does the compiler complain about >> when
one template is used inside another? 351
Chapter 26 Exception Tactics 353
FAQ 26.01: What belongs in a try block? 353
FAQ 26.02: When should a function catch an exception? 353
FAQ 26.03: Should a catch block fully recover from an error? 354
FAQ 26.04: How should a constructor handle a failure? 356
FAQ 26.05: What are zombie objects (and why should they be avoided)? 357
FAQ 26.06: What should an object do if one of its member objects could throw
an exception during its constructor? 357
FAQ 26.07: Should destructors throw exceptions when they fail? 359
FAQ 26.08: Should destructors call routines that may throw exceptions? 359
FAQ 26.09: Should resource deallocation primitives signal failure by throwing
an exception? 361
FAQ 26.10: What should the terminate() function do? 361
FAQ 26.11: What should the unexpected() function do? 361
FAQ 26.12: Under what circumstances can an overridden virtual member function
throw exceptions other than those listed by the specification of the member
function in the base class? 363
FAQ 26.13: How might the exception-handling mechanism cause a program to
silently crash? 365
Chapter 27 Types and RTTI 367
FAQ 27.01: What is the purpose of this chapter? 367
FAQ 27.02: What is static type checking? 367
FAQ 27.03: What is dynamic type checking? 368
FAQ 27.04: What is the basic problem with dynamic type checking? 370
FAQ 27.05: How can dynamic type checking be avoided? 371
FAQ 27.06: Are there better alternatives to dynamic type checking? 371
FAQ 27.07: What is a capability query? 373
FAQ 27.08: What is an alternative to dynamic type checking with containers?
373
FAQ 27.09: Are there cases where dynamic type checking is necessary? 374
FAQ 27.10: Given a pointer to an ABC, how can the class of the referent
be found? 374
FAQ 27.11: What is a downcast? 376
FAQ 27.12: What is an alternative to using downcasts? 378
FAQ 27.13: Why are downcasts dangerous? 380
FAQ 27.14: Should the inheritance graph of C++ hierarchies be tall or short?
380
FAQ 27.15: Should the inheritance graph of C++ hierarchies be monolithic
or a forest? 381
FAQ 27.16: What is Runtime Type Identification (RTTI)? 381
FAQ 27.17: What is the purpose of dynamic_cast<T>()?
381
FAQ 27.18: Is dynamic_cast<T>() a panacea? 383
FAQ 27.19: What does static_cast<T>() do? 383
FAQ 27.20: What does typeid() do? 383
FAQ 27.21: Are there any hidden costs for type-safe downcasts? 384
Chapter 28 Containers 387
FAQ 28.01: What are container classes and what are the most common mistakes
made with container classes? 387
FAQ 28.02: Are arrays good or evil? 388
FAQ 28.03: Should application development organizations create their own
container classes? 388
FAQ 28.04: What are some common mistakes with containers of pointers? 389
FAQ 28.05: Does this mean that containers of pointers should be avoided?
390
FAQ 28.06: Surely good old-fashioned char* is an exception,
right? 390
FAQ 28.07: Can auto_ptr<T> simplify ownership problems
with containers of pointers? 391
FAQ 28.08: Can a Java-like Object class simplify containers in C++? 391
FAQ 28.09: What's the difference between a homogeneous and a heterogeneous
container? 392
FAQ 28.10: Is it a good idea to use a "best of breed" approach when selecting
container classes? 392
FAQ 28.11: Should all projects use C++'s standardized containers? 393
FAQ 28.12: What are the C++ standardized container classes? 394
FAQ 28.13: What are the best applications for the standardized C++ sequence
container classes? 394
FAQ 28.14: What are the best situations for the standardized C++ associative
container classes? 398
Part IV Topics 401
Chapter 29 Mixing Overloading with Inheritance 403
FAQ 29.01: What is the difference between overloaded functions and overridden
functions? 403
FAQ 29.02: What is the hiding rule? 404
FAQ 29.03: How should the hiding rule be handled? 407
FAQ 29.04: What should a derived class do when it redefines some but not
all of a set of overloaded member functions inherited from the base class?
409
FAQ 29.05: Can virtual functions be overloaded? 411
Chapter 30 The Big Three 415
FAQ 30.01: What is the purpose of this chapter? 415
FAQ 30.02: What are the Big Three? 415
FAQ 30.03: What happens when an object is destroyed that doesn't have an
explicit destructor? 416
FAQ 30.04: What happens if an object is copied but doesn't have an explicit
copy constructor? 417
FAQ 30.05: What happens when an object that doesn't have an explicit assignment
operator is assigned? 418
FAQ 30.06: What is the Law of the Big Three? 420
FAQ 30.07: Which of the Big Three usually shows up first? 420
FAQ 30.08: What is remote ownership? 421
FAQ 30.09: How is remote ownership special? 422
FAQ 30.10: What if a class owns a referent and doesn't have all of the Big
Three? 422
FAQ 30.11: Are there any C++ classes that help manage remote ownership?
424
FAQ 30.12: Does auto_ptr enforce the Law of the Big Three and
solve the problems associated with remote ownership? 426
FAQ 30.13: Are there cases where one or two of the Big Three may be needed
but not all three? 428
FAQ 30.14: Are there any other circumstances that might explicitly warrant
the Big Three? 429
FAQ 30.15: Why does copying an object using memcpy() cause
a program crash? 430
FAQ 30.16: Why do programs with variable-length argument lists crash? 431
FAQ 30.17: Why do programs that use realloc() to reallocate
an array of objects crash? 432
Chapter 31 Using Objects to Prevent Memory Leaks 433
FAQ 31.01: When are memory leaks important? 433
FAQ 31.02: What is the easiest way to avoid memory leaks? 434
FAQ 31.03: What are the most important principles for resource management?
436
FAQ 31.04: Should the object that manages a resource also perform operations
that may throw exceptions? 437
FAQ 31.05: Should an object manage two or more resources? 438
FAQ 31.06: What if an object has a pointer to an allocation and one of the
object's member functions deletes the allocation? 440
FAQ 31.07: How should a pointer variable be handled after being passed to
delete? 440
FAQ 31.08: What should be done with a pointer to an object that is allocated
and deallocated in the same scope? 441
FAQ 31.09: How easy is it to implement reference counting with pointer semantics?
442
FAQ 31.10: Is reference counting with copy-on-write semantics hard to implement?
443
FAQ 31.11: How can reference counting be implemented with copy-on-write
semantics for a hierarchy of classes? 446
Chapter 32 Wild Pointers and Other Devilish Errors 451
FAQ 32.01: What is a wild pointer? 451
FAQ 32.02: What happens to a program that has even one wild pointer? 452
FAQ 32.03: What does the compiler mean by the warning "Returning a reference
to a local object"? 452
FAQ 32.04: How should pointers across block boundaries be controlled? 453
FAQ 32.05: Is the reference-versus-pointer issue influenced by whether or
not the object is allocated from the heap? 454
FAQ 32.06: When should C-style pointer casts be used? 455
FAQ 32.07: Is it safe to bind a reference variable to a temporary object?
455
FAQ 32.08: Should a parameter passed by const reference be
returned by const reference? 456
FAQ 32.09: Should template functions for things like min(x,y)
or abs(x) return a const reference? 457
FAQ 32.10: When is zero not necessarily zero? 457
Chapter 33 High-Performance Software 461
FAQ 33.01: Is bad performance a result of bad design or bad coding? 461
FAQ 33.02: What are some techniques for improving performance? 462
FAQ 33.03: What is an advantage of using pointers and references? 463
FAQ 33.04: What is a disadvantage of lots of references and pointers? 465
FAQ 33.05: How else can member objects improve performance over pointers?
467
FAQ 33.06: Which is better, ++i or i++? 468
FAQ 33.07: What is the performance difference between Fred x(5);
and Fred y = 5; and Fred z = Fred(5);? 470
FAQ 33.08: What kinds of applications should consider using final classes
and final member functions? 471
FAQ 33.09: What is a final class? 471
FAQ 33.10: What is a final member function? 472
FAQ 33.11: How can final classes and final member functions improve performance?
473
FAQ 33.12: When should a nonfinal virtual function be invoked with a fully
qualified name? 474
FAQ 33.13: Should full qualification be used when calling another member
function of the same class? 475
FAQ 33.14: Do final classes and final member functions cause a lot of code
duplication? 476
FAQ 33.15: Why do some developers dislike final member functions and final
classes? 478
FAQ 33.16: Can a programming language--rather than just the compiler--affect
the performance of software? 478
Chapter 34 COM and Active X 481
FAQ 34.01: Who should read this chapter? 481
FAQ 34.02: What is the Component Object Model? 481
FAQ 34.03: What are ActiveX and OLE? 482
FAQ 34.04: What does the name Component Object Model mean? 483
FAQ 34.05: What is a "binary object model"? 483
FAQ 34.06: What are the key features of COM? 484
FAQ 34.07: What are GUIDs? 485
FAQ 34.08: Why does COM need GUIDs (and CLSIDs and IIDs)? 486
FAQ 34.09: What is an interface? 487
FAQ 34.10: What is the IUnknown interface? 488
FAQ 34.11: How many ways are there to specify COM interfaces? 490
FAQ 34.12: What are COM classes and COM objects? 492
FAQ 34.13: How hard is it for callers to create and use a COM object? 496
FAQ 34.14: How does COM provide language transparency? 498
FAQ 34.15: How does COM provide location transparency? 499
FAQ 34.16: What types of errors occur due to reference counting? 500
FAQ 34.17: What mechanism does COM define for error handling? 501
FAQ 34.18: How are interfaces versioned? 502
FAQ 34.19: Is COM object oriented? 503
FAQ 34.20: What is the biggest problem with COM? 504
FAQ 34.21: What are the major differences between COM and C++? 504
FAQ 34.22: When should a class be defined as a COM class? 508
FAQ 34.23: What is Automation? 509
FAQ 34.24: What are dispatch interfaces? 510
FAQ 34.25: When should a class expose a Dispatch interface? 511
FAQ 34.26: How does Automation work? 512
FAQ 34.27: How does Invoke accomplish all of this? 514
FAQ 34.28: What is a type library? 514
FAQ 34.29: What are the benefits of using type libraries? 515
FAQ 34.30: How do type libraries improve performance? 516
FAQ 34.31: What are dual interfaces? 516
FAQ 34.32: What limitations are there on dual interfaces? 518
FAQ 34.33: What are OLE custom controls and ActiveX controls? 519
FAQ 34.34: Why do ActiveX controls differ from OLE custom controls? 520
FAQ 34.35: What is a control container? 521
FAQ 34.36: What are component categories? 522
FAQ 34.37: What are events? 522
FAQ 34.38: What is DCOM? 523
FAQ 34.39: How stable is DCOM's infrastructure? 524
FAQ 34.40: What is COM+? 524
Chapter 35 Transitioning to CORBA 527
FAQ 35.01: What is CORBA? 527
FAQ 35.02: What is an ORB? 527
FAQ 35.03: What is IDL? 528
FAQ 35.04: What is COS? 528
FAQ 35.05: What is OMA? 528
FAQ 35.06: What is OMG? 529
FAQ 35.07: What is the purpose of this chapter? 529
FAQ 35.08: What is the most important message of this chapter? 529
FAQ 35.09: What are the important concepts behind CORBA? 530
FAQ 35.10: Isn't OMG IDL pretty much the same as C++? 531
FAQ 35.11: Is the lifecycle of a CORBA object the same as the lifecycle
of a C++ object? 532
FAQ 35.12: Is the C++ code that interacts with the CORBA implementation
portable to a different CORBA vendor? 533
FAQ 35.13: How do CORBA exceptions compare to C++ exceptions? 533
FAQ 35.14: Which CORBA implementation is best? Is CORBA better than COM?
534
Chapter 36 C Language Considerations 535
FAQ 36.01: What are the main issues when mixing C and C++ code in the same
application? 535
FAQ 36.02: How can C++ code call C code? 536
FAQ 36.03: How can C code call C++ code? 537
FAQ 36.04: Why is the linker giving errors for C functions called from C++
functions and vice versa? 538
FAQ 36.05: How can an object of a C++ class be passed to or from a C function?
538
FAQ 36.06: Can a C function directly access data in an object of a C++ class?
540
FAQ 36.07: Can C++ I/O (<iostream>) be mixed with C I/O
(<stdio.h>)? 541
FAQ 36.08: Which is safer: <iostream> or <stdio.h>?
541
FAQ 36.09: Which is more extensible: <iostream> or <stdio.h>?
542
FAQ 36.10: Which is more flexible: <iostream> or <stdio.h>?
542
FAQ 36.11: Why does it seem that C++ programming feels farther away from
the machine than C? 543
FAQ 36.12: Why does C++ do more things behind your back than C does? 543
Chapter 37 Private and Protected Inheritance 545
FAQ 37.01: What are private inheritance and protected inheritance? 545
FAQ 37.02: What is the difference between private inheritance and protected
inheritance? 546
FAQ 37.03: What is the syntax and semantics for private and protected inheritance?
547
FAQ 37.04: When should normal has-a be used, rather than private or protected
inheritance? 548
FAQ 37.05: What are the access rules for public, protected, and private
inheritance? 549
FAQ 37.06: In a private or protected derived class, how can a member function
that was public in the base class be made public in the derived class? 550
FAQ 37.07: Should a pointer be cast from a private or protected derived
class to its base class? 551
Chapter 38 Pointers to Member Functions 553
FAQ 38.01: What is the type of a pointer to a nonstatic member function?
553
FAQ 38.02: Can pointers to nonstatic member functions be passed to signal
handlers, X event call-back handlers, and so on, that expect C-like function
pointers? 555
FAQ 38.03: What is one of the most common errors when using pointers to
member functions? 556
FAQ 38.04: How is an array of pointers to nonstatic member functions declared?
556
Chapter 39 The Transition to OO and C++ 559
FAQ 39.01: Who should read this chapter? 559
FAQ 39.02: What are the key messages of this chapter? 559
FAQ 39.03: How many stages of technical expertise are there in the transition?
560
FAQ 39.04: Why bother defining levels of expertise? 561
FAQ 39.05: Can OO/C++ be learned from a book? 561
FAQ 39.06: Can OO/C++ be learned from a course? 562
FAQ 39.07: What is the key to learning OO/C++? 562
FAQ 39.08: Are small projects a good way to get your feet wet? 563
FAQ 39.09: Does being a good C programmer help when learning OO/C++? 564
FAQ 39.10: What skills help when learning OO/C++? 565
Customer Reviews
Customer Reviews: 1 Average Customer Rating:      Jun 5, 2001     Lawrence Lee (lawrence_lee@engineer.com) from CA, US Precise and Useful There are two ways to use this book: as a quick reference or as a tutorial.
The FAQ format makes topic lookups easy. The questions are day to day questions such as "Should destructors throw exceptions when they fail?" and "What should assignment operator returns?". These questions should be asked every time a C++ statement is written. The extensive index is one of its strengths.
Most readers use it as a quick reference; however, C++ FAQ is in fact a very good C++ and software engineering tutorial. It starts from outside-in, explaining the management and architectural perspectives. Then it addresses object-oriented design and language features. This is the flow a new programmer should follow to learn C++.
All the answers are so precise that most of the answers are within 3/4 of a page each. The reader usually gets what s/he wants just by reading that, and s/he would read some more when another question arises.
|
 |