Nytro Posted September 21, 2011 Report Posted September 21, 2011 C++ FAQs (Second Edition)Authors:- Marshall Cline - Greg Lomow - Mike GirouData: May 2000ContentsI Preliminaries 11 Introduction 31.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 31.2 What are C++ FAQs? . . . . . . . . . . . . . . . . . . . . . . . . 31.3 Who is the target audience for this book? . . . . . . . . . . . . . 31.4 Is this a book about C++ per se? . . . . . . . . . . . . . . . . . 41.5 Why do developers need a guidebook for C++ and OO technology? 41.6 What kind of guidance is given in the answers to these FAQs? . . 51.7 What is the electronic FAQ and why buy this book when theelectronic FAQ is free? . . . . . . . . . . . . . . . . . . . . . . . . 51.8 Why should you buy this edition if you already have a copy ofthe first edition? . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.9 What conventions are used in this book? . . . . . . . . . . . . . . 62 Basic C++ Syntax and Semantics 92.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 92.2 What are the basics of main()? . . . . . . . . . . . . . . . . . . . 92.3 What are the basics of functions? . . . . . . . . . . . . . . . . . . 102.4 What are the basics of default parameters? . . . . . . . . . . . . 102.5 What are the basics of local (auto) objects? . . . . . . . . . . . . 112.6 What are the basics of constructing objects using explicit parameters?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122.7 What are the basics of dynamically allocated (new) objects? . . . 132.8 What are the basics of local objects within inner scopes? . . . . . 142.9 What are the basics of passing objects by reference? . . . . . . . 152.10 What are the basics of passing objects by value? . . . . . . . . . 162.11 What are the basics of passing objects by pointer? . . . . . . . . 172.12 What are the basics of stream output? . . . . . . . . . . . . . . . 182.13 What are the basics of stream input? . . . . . . . . . . . . . . . . 192.14 What are the basics of using classes that contain overloaded operators?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202.15 What are the basics of using container classes? . . . . . . . . . . 212.16 What are the basics of creating class header files? . . . . . . . . . 222.17 What are the basics of defining a class? . . . . . . . . . . . . . . 232.18 What are the basics of defining member functions? . . . . . . . . 242.19 What are the basics of adding a constructor to a class? . . . . . . 252.20 What are the basics of adding a destructor to a class? . . . . . . 27ii2.21 What are the basics of defining a class that contains a pointer toan object allocated from the heap? . . . . . . . . . . . . . . . . . 282.22 What are the basics of global objects? . . . . . . . . . . . . . . . 302.23 What are the basics of throwing and catching exceptions? . . . . 322.24 What are the basics of inheritance and dynamic binding? . . . . 343 Understanding the Management Perspective 373.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 373.2 What is the core message of this chapter (and this book)? . . . . 373.3 Why are the managers in charge rather than the developers whounderstand technology? . . . . . . . . . . . . . . . . . . . . . . . 383.4 How can someone manager something they don’t understand? . . 393.5 What is the most common mistake on C++ and OO projects? . 393.6 What’s the “Software Peter Principle”? . . . . . . . . . . . . . . 403.7 Should an organization use OO on all its projects? . . . . . . . . 403.8 Can OO be ignored until it goes away? . . . . . . . . . . . . . . . 413.9 What OO language is best? . . . . . . . . . . . . . . . . . . . . . 413.10 What is the right approach to processes and tools? . . . . . . . . 423.11 What is the right approach with off-the-shelf class libraries andframeworks? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434 The Architectural Perspective 444.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 444.2 Why is software architecture important? . . . . . . . . . . . . . . 444.3 What should the architecture be based on, the problem beingsolved or the problem domain? . . . . . . . . . . . . . . . . . . . 454.4 Should the software architecture be based on the policy of theproblem? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464.5 Do customers ever change their requirements? . . . . . . . . . . . 474.6 Are stable requirements desirable? . . . . . . . . . . . . . . . . . 474.7 What is the key to planning for change? . . . . . . . . . . . . . . 474.8 What is a framework? . . . . . . . . . . . . . . . . . . . . . . . . 484.9 What is the “inversion of control” exhibited by frameworks? . . . 484.10 What is an extensible, domain-specific framework? . . . . . . . . 494.11 What characteristics make a framework extensible yet domainspecific?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494.12 What happens if the domain analysis is incorrect? . . . . . . . . 504.13 How much effort should be expended to support change — thatis, how much is extensibility worth? . . . . . . . . . . . . . . . . 504.14 How does an architect make the software architecture flexible? . 514.15 What is the secret to achieving reuse? . . . . . . . . . . . . . . . 52II Object-Oriented Design 535 Object-Oriented Fundamentals 555.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 555.2 Why is the world adopting OO technology? . . . . . . . . . . . . 555.3 What are some of the benefits of using C++ for OO programming? 565.4 What are the fundamental concepts of object-oriented technology? 56iii5.5 Why are classes important? . . . . . . . . . . . . . . . . . . . . . 575.6 What is an object? . . . . . . . . . . . . . . . . . . . . . . . . . . 585.7 What are the desirable qualities of an object? . . . . . . . . . . . 585.8 How are classes better than the three basic building blocks ofprocedural software? . . . . . . . . . . . . . . . . . . . . . . . . . 595.9 What is the purpose of composition? . . . . . . . . . . . . . . . . 605.10 What is the purpose of inheritance? . . . . . . . . . . . . . . . . 615.11 What are the advantages of polymorphism and dynamic binding? 615.12 How does OO help produce flexible and extensible software? . . . 625.13 How can old code call new code? . . . . . . . . . . . . . . . . . . 625.14 What is an abstraction and why is it important? . . . . . . . . . 655.15 Should abstractions be user-centric or developer-centric? . . . . . 665.16 What’s the difference between encapsulation and abstraction? . . 665.17 What are the consequences of encapsulating a bad abstraction? . 675.18 What’s the value of separating interface from implementation? . 675.19 How can separating interface from implementation improve performanceas well as flexibility? . . . . . . . . . . . . . . . . . . . 685.20 What is the best way to create a good interface to an abstraction? 695.21 How are get/set member functions related to poorly designedinterfaces? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705.22 Should there be a get and a set member function for each memberdatum? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705.23 Is the real purpose of a class to export data? . . . . . . . . . . . 715.24 Should OO be viewed as data-centric? . . . . . . . . . . . . . . . 716 Specification of Observable Behavior 736.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 736.2 Should users of a member function rely on what the code actuallydoes or on the specification? . . . . . . . . . . . . . . . . . . . . . 736.3 What are the advantages of relying on the specification ratherthan the implementation? . . . . . . . . . . . . . . . . . . . . . . 756.4 What are advertised requirements and advertised promises? . . . 766.5 How are the advertised requirements and advertised promises ofthe member functions specified? . . . . . . . . . . . . . . . . . . . 766.6 Why are changes feared in development organizations that don’tuse specification? . . . . . . . . . . . . . . . . . . . . . . . . . . . 786.7 How do developers determine if a proposed change will breakexisting code? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796.8 What are the properties of a substitutable (backward compatible)change in a specification? . . . . . . . . . . . . . . . . . . . . . . 796.9 How can it be shown that the implementation of a member functionfulfills its specification? . . . . . . . . . . . . . . . . . . . . . 806.10 Is it possible to keep the specification synchronized with the code? 817 Proper Inheritance 837.1 What is proper inheritance? . . . . . . . . . . . . . . . . . . . . . 837.2 What are the benefits of proper inheritance? . . . . . . . . . . . 847.3 What is improper inheritance? . . . . . . . . . . . . . . . . . . . 857.4 Isn’t the difference between proper and improper inheritance obvious?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85iv7.5 Is substitutability based on what the code does or what the specificationpromises the code will do? . . . . . . . . . . . . . . . . . 867.6 Is it proper to revoke (hide) an inherited public: member function? 887.7 What is specialization? . . . . . . . . . . . . . . . . . . . . . . . . 887.8 What do subsets have to do with proper inheritance? . . . . . . . 898 Detecting and Correcting Improper Inheritance 908.1 Can improper inheritance wreck a project? . . . . . . . . . . . . 908.2 What’s the best way to learn how to avoid improper inheritance? 908.3 Is intuition a reliable guide to understanding proper inheritance? 918.4 Is an Ostrich a kind-of Bird? . . . . . . . . . . . . . . . . . . . 918.5 Should an overridden virtual function throw an exception? . . . . 938.6 Can an overridden virtual function be a no-op? . . . . . . . . . . 958.7 Why does C++ make it so hard to fix the Ostrich/Bird dilemma? 968.8 Should Circle inherit from Ellipse? . . . . . . . . . . . . . . . 978.9 What can be done about the asymmetric-circle dilemma? . . . . 998.10 What is the one issue in these FAQs that doesn’t seem to die? . 1008.11 Should Stack inherit from List? . . . . . . . . . . . . . . . . . . 1018.12 Is code reuse the main purpose of inheritance? . . . . . . . . . . 1028.13 Is container-of-thing a kind-of container-of-anything? . . . . . . . 1038.14 Is bag-of-apple a kind-of bag-of-fruit, assuming bag-of-fruit allowsthe insertion of any kind-of fruit? . . . . . . . . . . . . . . . . . . 1048.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?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1078.16 Is array-of Derived a kind-of Base? . . . . . . . . . . . . . . . . 1088.17 Does the fact that an array-of Derived can be passed as an arrayofBase mean that arrays are bad? . . . . . . . . . . . . . . . . . 1109 Error Handling Strategies 1119.1 Is error handling a major source of fundamental mistakes? . . . . 1119.2 How should runtime errors be handled in C++? . . . . . . . . . 1119.3 What happens to objects in stack frames that become unwoundduring the throw/catch process? . . . . . . . . . . . . . . . . . . 1129.4 What is an exception specification? . . . . . . . . . . . . . . . . . 1129.5 What are the disadvantages of using return codes for error handling?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1139.6 What are the advantages of throw...catch? . . . . . . . . . . . 1139.7 Why is it helpful to separate normal logic from exception handlinglogic? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1149.8 What is the hardest part of using exception handling? . . . . . . 1179.9 When should a function throw an exception? . . . . . . . . . . . 1189.10 What is the best approach for the hierarchy of exception objects? 1209.11 How should exception classes be named? . . . . . . . . . . . . . . 1219.12 Where do setjmp and longjmp belong in C++? . . . . . . . . . 12210 Testing Strategies 12310.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 12310.2 What are the advantages of self-testing objects? . . . . . . . . . . 123v10.3 What are some common excuses people use for not building selftestinginto their objects? . . . . . . . . . . . . . . . . . . . . . . 12410.4 What will happen if techniques like those presented here are notused? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12510.5 When is a class correct? . . . . . . . . . . . . . . . . . . . . . . . 12510.6 What is behavioral self-testing? . . . . . . . . . . . . . . . . . . . 12610.7 What is a class invariant? . . . . . . . . . . . . . . . . . . . . . . 12810.8 Why should the invariant be captured explicitly? . . . . . . . . . 12910.9 When should the testInvariant() member function be called? 13010.10What can be done to ensure that an object doesn’t get blownaway by a wild pointer? . . . . . . . . . . . . . . . . . . . . . . . 130III Language Facilities 13311 References 13511.1 What is a reference? . . . . . . . . . . . . . . . . . . . . . . . . . 13511.2 What does “referent” mean? . . . . . . . . . . . . . . . . . . . . 13611.3 When can a reference be attached to its referent? . . . . . . . . . 13611.4 What happens when a value is assigned to a reference? . . . . . . 13611.5 What is a local reference? . . . . . . . . . . . . . . . . . . . . . . 13711.6 What does it mean to return a reference? . . . . . . . . . . . . . 13711.7 What is the result of taking the address of a reference? . . . . . . 13811.8 Can a reference be made to refer to a different referent? . . . . . 13911.9 Why use references when pointers can do everything referencescan do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13911.10Aren’t references just pointers in disguise? . . . . . . . . . . . . . 14011.11When are pointers needed? . . . . . . . . . . . . . . . . . . . . . 14011.12Why do some people hate references? . . . . . . . . . . . . . . . . 14111.13Does int& const x make sense? . . . . . . . . . . . . . . . . . . 14112 New and Delete 14312.1 Does new do more than allocate memory? . . . . . . . . . . . . . 14312.2 Why is new better than good old trustworthy malloc()? . . . . . 14312.3 Does C++ have a counterpart to realloc() that goes along withnew and delete? . . . . . . . . . . . . . . . . . . . . . . . . . . . 14412.4 Can pointers returned from new be deallocated with free()? Canpointers returned from malloc() be deallocated with delete? . 14412.5 Does delete p delete the pointer p or the referent *p? . . . . . . 14412.6 Should the pointer returned from new Fred() be checked to seeif it is NULL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14512.7 How can new be convinced to return NULL rather than throw anexception? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14512.8 How can new be set up to automatically flush pools of recycledobjects whenever memory runs low? . . . . . . . . . . . . . . . . 14612.9 What happens if delete p is called when p is NULL? . . . . . . . 14912.10What happens when a pointer is deleted twice? . . . . . . . . . . 15012.11How can an array of things be allocated and deallocated? . . . . 15012.12What if delete p (not delete[] p) is used to delete an arrayallocated via new Fred[n]? . . . . . . . . . . . . . . . . . . . . . 151vi12.13Can the [] of delete[] p be dropped when p points to an arrayof some built-in type such as char? . . . . . . . . . . . . . . . . . 15112.14How is an object constructed at a predetermined position in memory?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15212.15How can class Fred guarantee that Fred objects are created onlywith new and not on the stack? . . . . . . . . . . . . . . . . . . . 15312.16How are objects created by placement new destroyed? . . . . . . 15412.17In p = new Fred(), does the Fred memory “leak” if the Fredconstructor throws an exception? . . . . . . . . . . . . . . . . . . 15512.18Is it legal (and moral) for a member function to say delete this?15612.19After p=new Fred[n], how does the compiler know that there aren objects to be destructed during delete[] p? . . . . . . . . . . 15713 Inline Functions 15813.1 What is the purpose of inline functions? . . . . . . . . . . . . . 15813.2 What is the connection between the keyword “inline” and “inlined”functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . 15913.3 Are there any special rules about inlining? . . . . . . . . . . . . . 15913.4 What is the one-definition rule (ODR)? . . . . . . . . . . . . . . 15913.5 What are some performance considerations with inline functions?16013.6 Do inlined functions improve performance? . . . . . . . . . . . . 16013.7 Do inlined functions increase the size of the executable code? . . 16213.8 Why shouldn’t the inlining decision be made when the code isfirst written? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16313.9 What happens when a programmer uses an inlined function obtainedfrom a third party? . . . . . . . . . . . . . . . . . . . . . . 16313.10Is there an easy way to swap between inline and non-inlinecode? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16414 Const Correctness 16714.1 How should pointer declarations be read? . . . . . . . . . . . . . 16714.2 How can C++ programmers avoid making unexpected changesto objects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16714.3 Does const imply runtime overhead? . . . . . . . . . . . . . . . . 16914.4 Does const allow the compiler to generate more efficient code? . 16914.5 Is const correctness tedious? . . . . . . . . . . . . . . . . . . . . 16914.6 Why should const correctness be done sooner rather than later? 17014.7 What’s the difference between an inspector and a mutator? . . . 17114.8 When should a member function be declared as const? . . . . . 17114.9 Does const apply to the object’s bitwise state or its abstract state?17214.10When should const not be used in declaring formal parameters? 17314.11When should const not be used in declaring a function returntype? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17414.12How can a “nonobservable” data member be updated within aconst member function? . . . . . . . . . . . . . . . . . . . . . . . 17514.13Can an object legally be changed even though there is a constreference (pointer) to it? . . . . . . . . . . . . . . . . . . . . . . . 17614.14Does const cast mean lost optimization opportunities? . . . . . 177vii15 Namespaces 17815.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 17815.2 What is a namespace? . . . . . . . . . . . . . . . . . . . . . . . . 17815.3 How can code outside a namespace use names declared withinthat namespace? . . . . . . . . . . . . . . . . . . . . . . . . . . . 17915.4 What happens if two namespaces contain the same name? . . . . 18015.5 What are some of the rules for using namespaces? . . . . . . . . 18115.6 What is name lookup? . . . . . . . . . . . . . . . . . . . . . . . . 18215.7 What are the tradeoffs between the various techniques for usingnames from a namespace, particularly the standard namespace? . 18215.8 Can namespaces break code? . . . . . . . . . . . . . . . . . . . . 18315.9 Do namespaces have any other applications? . . . . . . . . . . . . 18415.10How do namespaces solve the problem of long identifiers? . . . . 18416 Using Static 18516.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 18516.2 What are static class members? . . . . . . . . . . . . . . . . . . . 18516.3 What is an analogy for static data members? . . . . . . . . . . . 18616.4 Can inline functions safely access static data members? . . . . 18816.5 What is an analogy for static member functions? . . . . . . . . . 18916.6 How is a static data member similar to a global variable? . . . . 19116.7 How is a static member function similar to a friend function? . . 19216.8 What is the named constructor idiom? . . . . . . . . . . . . . . . 19216.9 How should static member functions be called? . . . . . . . . . . 19316.10Why might a class with static data members get linker errors? . 19416.11How is a const static data member initialized? . . . . . . . . . . 19416.12What is the right strategy for implementing a function that needsto maintain state between calls? . . . . . . . . . . . . . . . . . . 19516.13How can the function call operator help with functionoids? . . . 19716.14Is it safe to be ignorant of the static initialization order problem? 19816.15What is a simple and robust solution to the static initializationorder problem? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19916.16What if the static object’s destructor has important side effectsthat must eventually occur? . . . . . . . . . . . . . . . . . . . . . 20016.17What if the static object’s destructor has important side effectsthat must eventually occur and the static object must be accessedby another static object’s destructor? . . . . . . . . . . . . . . . . 20116.18What are some criteria for choosing between all these varioustechniques? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20317 Derived Classes 20517.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 20517.2 How does C++ express inheritance? . . . . . . . . . . . . . . . . 20517.3 What is a concrete derived class? . . . . . . . . . . . . . . . . . . 20617.4 Why can’t a derived class access the private: members of itsbase class? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20717.5 How can a base class protect derived classes so that changes tothe base class will not affect them? . . . . . . . . . . . . . . . . . 20917.6 Can a derived class pointer be converted into a pointer to itspublic base class? . . . . . . . . . . . . . . . . . . . . . . . . . . . 210viii17.7 How can a class Y be a kind-of another class X as well as gettingthe bits of X? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21017.8 How can a class Y get the bits of an existing class X withoutmaking Y a kind-of X? . . . . . . . . . . . . . . . . . . . . . . . . 21117.9 How can a class Y be a kind-of another class X without inheritingthe bits of X? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21218 Access Control 21318.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 21318.2 How are private:, protected:, and public: different? . . . . . 21318.3 Why can’t subclasses access the private: parts of their base class?21318.4 What’s the difference between the keywords struct and class? 21418.5 When should a data member be protected: rather than private:?21418.6 Why is private: the default access level for a class? . . . . . . . 21519 Friend Classes and Friend Functions 21619.1 What is a friend? . . . . . . . . . . . . . . . . . . . . . . . . . . . 21619.2 What’s a good mental model for friend classes? . . . . . . . . . . 21719.3 What are some advantages of using friend classes? . . . . . . . . 21819.4 Do friends violate the encapsulation barrier? . . . . . . . . . . . 21819.5 What is a friend function? . . . . . . . . . . . . . . . . . . . . . . 21819.6 When should a function be implemented as a friend functionrather than a member function? . . . . . . . . . . . . . . . . . . . 21919.7 What are some guidelines to make sure friend functions are usedproperly? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22019.8 What does it mean that friendship isn’t transitive? . . . . . . . . 22019.9 What does it mean that friendship isn’t inherited? . . . . . . . . 22219.10What does it mean that friends aren’t virtual? . . . . . . . . . . 22319.11What qualities suggest a friend function rather than a memberfunction? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22519.12Should friend functions be declared in the private:, protected:,or public: section of a class? . . . . . . . . . . . . . . . . . . . . 22719.13What is a private class? . . . . . . . . . . . . . . . . . . . . . . . 22719.14How are objects of a class printed? . . . . . . . . . . . . . . . . . 22919.15How do objects of a class receive stream input? . . . . . . . . . . 23020 Constructors and Destructors 23120.1 What is the purpose of a constructor? . . . . . . . . . . . . . . . 23120.2 What is C++’s constructor discipline? . . . . . . . . . . . . . . . 23120.3 What is the purpose of a destructor? . . . . . . . . . . . . . . . . 23120.4 What is C++’s destructor discipline? . . . . . . . . . . . . . . . . 23120.5 What happens when a destructor is executed? . . . . . . . . . . . 23120.6 What is the purpose of a copy constructor? . . . . . . . . . . . . 23120.7 When is a copy constructor invoked? . . . . . . . . . . . . . . . . 23120.8 What is the “default constructor”? . . . . . . . . . . . . . . . . . 23220.9 Should one constructor call another constructor as a primitive? . 23220.10Does the destructor for a derived class need to explicitly call thedestructor of its base class? . . . . . . . . . . . . . . . . . . . . . 23220.11How can a local object be destructed before the end of its function?232ix20.12What is a good way to provide intuitive, multiple constructorsfor a class? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23220.13When the constructor of a base class calls a virtual function, whyisn’t the override called? . . . . . . . . . . . . . . . . . . . . . . . 23220.14When a base class destructor calls a virtual function, why isn’tthe override called? . . . . . . . . . . . . . . . . . . . . . . . . . . 23220.15What is the purpose of placement new? . . . . . . . . . . . . . . 23221 Virtual Functions 23321.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 23321.2 What is a virtual member function? . . . . . . . . . . . . . . . . 23321.3 How much does it cost to call a virtual function compared tocalling a normal functions? . . . . . . . . . . . . . . . . . . . . . 23321.4 How does C++ perform static typing while supporting dynamicbinding? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23321.5 Can destructors be virtual? . . . . . . . . . . . . . . . . . . . . . 23321.6 What is the purpose of a virtual destructor? . . . . . . . . . . . . 23321.7 What is a virtual constructor? . . . . . . . . . . . . . . . . . . . 23421.8 What syntax should be used when a constructor or destructorcalls a virtual function in its project? . . . . . . . . . . . . . . . . 23421.9 Should the scope operator :: be used when invoking virtualmember functions? . . . . . . . . . . . . . . . . . . . . . . . . . . 23421.10What is a pure virtual member function? . . . . . . . . . . . . . 23421.11Can a pure virtual function be defined in the same class thatdeclares it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23421.12How should a virtual destructor be defined when it has no code? 23421.13Can an ABC have a pure virtual destructor? . . . . . . . . . . . 23421.14How can the compiler be kept from generating duplicate out-linedcopies of inline virtual functions? . . . . . . . . . . . . . . . . . . 23421.15Should a class with virtual functions have at least one non-inlinevirtual function? . . . . . . . . . . . . . . . . . . . . . . . . . . . 23522 Initialization Lists 23622.1 What are constructor initialization lists? . . . . . . . . . . . . . . 23622.2 What will happen if constructor initialization lists are not used? 23622.3 What’s the guideline for using initialization lists in constructordefinitions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23622.4 Is it normal for constructors to have nothing inside their body? . 23622.5 How is a const data member initialized? . . . . . . . . . . . . . . 23622.6 How is a reference data member initialized? . . . . . . . . . . . . 23622.7 Are initializers executed in the same order in which they appearin the initialization list? . . . . . . . . . . . . . . . . . . . . . . . 23722.8 How should initializers be ordered in a constructor’s initializationlist? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23722.9 Is it normal for one member object to be initialized using anothermember object in the constructor’s initialization list? . . . . . . . 23722.10What if one member object has to be initialized using anothermember object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23722.11Are there exceptions to the rule “Initialize all member objects inan initialization list”? . . . . . . . . . . . . . . . . . . . . . . . . 237x22.12How can an array of objects be initialized with specific initializers?23723 Operator Overloading 23823.1 Are overloaded operators like normal functions? . . . . . . . . . . 23823.2 When should operator overloading be used? . . . . . . . . . . . . 23823.3 What operators can’t be overloaded? . . . . . . . . . . . . . . . . 23823.4 Is the goal of operator overloading to make the class easier tounderstand? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23823.5 Why do subscript operators usually come in pairs? . . . . . . . . 23823.6 What is the most important consideration for operators such as+=, +, and =? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23823.7 How are the prefix and postfix versions of operator++ distinguished?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23923.8 What should the prefix and postfix versions of operator++ return?23923.9 How can a Matrix-like class have a subscript operator that takesmore than one subscript? . . . . . . . . . . . . . . . . . . . . . . 23923.10Can a ** operator serve as an exponentiation operator? . . . . . 23924 Assignment Operators 24024.1 What should assignment operators return? . . . . . . . . . . . . 24024.2 What is wrong with an object being assigned to itself? . . . . . . 24024.3 What should be done about self-assignment? . . . . . . . . . . . 24024.4 Should an assignment operator throw an exception after partiallyassigning an object? . . . . . . . . . . . . . . . . . . . . . . . . . 24024.5 How should the assignment operator be declared in an ABC? . . 24024.6 When should a user-defined assignment operator mimic the assignmentoperator that the compiler would generate automatically?24024.7 What should be returned by private: and protected: assignmentoperators? . . . . . . . . . . . . . . . . . . . . . . . . . . . 24124.8 Are there techniques that increase the likelihood that the compilersynthesizedassignment operator will be right? . . . . . . . . . . . 24124.9 How should the assignment operator in a derived class behave? . 24124.10Can an ABC’s assignment operator be virtual? . . . . . . . . . 24124.11What should a derived class do if a base class’s assignment operatoris virtual? . . . . . . . . . . . . . . . . . . . . . . . . . . . 24124.12Should the assignment operator be implemented by using placementnew and the copy constructor? . . . . . . . . . . . . . . . . 24125 Templates 24225.1 What is the purpose of templates? . . . . . . . . . . . . . . . . . 24225.2 What are the syntax and semantics for a class template? . . . . . 24225.3 How can a template class be specialized to handle special cases? 24425.4 What are the syntax and semantics for a function template? . . . 24725.5 Should a template use memcpy() to copy objects of its templateargument? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24725.6 Why does the compiler complain about >> when one template isused inside another? . . . . . . . . . . . . . . . . . . . . . . . . . 249xi26 Exception Tactics 25126.1 What belongs in a try block? . . . . . . . . . . . . . . . . . . . . 25126.2 When should a function catch an exception? . . . . . . . . . . . 25126.3 Should a catch block fully recover from an error? . . . . . . . . . 25226.4 How should a constructor handle a failure? . . . . . . . . . . . . 25426.5 What are zombie objects (and why should they be avoided)? . . 25426.6 What should an object do if one of its member objects couldthrow an exception during its constructor? . . . . . . . . . . . . . 25526.7 Should destructors throw exceptions when they fail? . . . . . . . 25726.8 Should destructors call routines that may throw exception? . . . 25726.9 Should resource deallocation primitives signal failure by throwingan exception? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25826.10What should the terminate() function do? . . . . . . . . . . . . 25826.11What should the unexpected() function do? . . . . . . . . . . . 25926.12Under what circumstances can an overridden virtual memberfunction throw exceptions other than those listed by the specificationof the member function in the base class? . . . . . . . . 26126.13How might the exception-handling mechanism cause a programto silently crash? . . . . . . . . . . . . . . . . . . . . . . . . . . . 26227 Types and RTTI 26427.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 26427.2 What is static type checking? . . . . . . . . . . . . . . . . . . . . 26427.3 What is dynamic type checking? . . . . . . . . . . . . . . . . . . 26527.4 What is the basic problem with dynamic type checking? . . . . . 26727.5 How can dynamic type checking be avoided? . . . . . . . . . . . 26727.6 Are there better alternatives to dynamic type checking? . . . . . 26827.7 What is a capability query? . . . . . . . . . . . . . . . . . . . . . 26927.8 What is an alternative to dynamic type checking with containers? 26927.9 Are there cases where dynamic type checking is necessary? . . . 27027.10Given a pointer to an ABC, how can the class of the referent befound? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27127.11What is a downcast? . . . . . . . . . . . . . . . . . . . . . . . . . 27327.12What is an alternative to using downcasts? . . . . . . . . . . . . 27427.13Why are downcasts dangerous? . . . . . . . . . . . . . . . . . . . 27627.14Should the inheritance graph of C++ hierarchies be tall or short? 27627.15Should the inheritance graph of C++ hierarchies be monolithicor a forest? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27727.16What is Runtime Type Identification (RTTI)? . . . . . . . . . . 27727.17What is the purpose of dynamic cast<T>()? . . . . . . . . . . . 27727.18Is dynamic cast<T>() a panacea? . . . . . . . . . . . . . . . . . 27827.19What does static cast<T>() do? . . . . . . . . . . . . . . . . . 27927.20What does typeid() do? . . . . . . . . . . . . . . . . . . . . . . 27927.21Are there any hidden costs for type-safe downcasts? . . . . . . . 27928 Containers 28128.1 What are container classes and what are the most common mistakesmade with container classes? . . . . . . . . . . . . . . . . . 28128.2 Are arrays good or evil? . . . . . . . . . . . . . . . . . . . . . . . 282xii28.3 Should application development organizations create their owncontainer classes? . . . . . . . . . . . . . . . . . . . . . . . . . . . 28228.4 What are some common mistakes with containers of pointers? . . 28328.5 Does this mean that containers of pointers should be avoided? . . 28328.6 Surely good old-fashioned char* is an exception, right? . . . . . 28328.7 Can auto ptr<T> simplify ownership problems with containersof pointers? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28428.8 Can a Java-like Object class simplify containers in C++? . . . . 28428.9 What’s the difference between a homogeneous and a heterogeneouscontainer? . . . . . . . . . . . . . . . . . . . . . . . . . . . 28528.10Is it a good idea to use a “best of breed” approach when selectingcontainer classes? . . . . . . . . . . . . . . . . . . . . . . . . . . . 28528.11Should all projects use C++’s standardized containers? . . . . . 28628.12What are the C++ standardized container classes? . . . . . . . . 28628.13What are the best applications for the standardized C++ sequencecontainer classes? . . . . . . . . . . . . . . . . . . . . . . 28728.14What are the best situations for the standardized C++ associativecontainer classes? . . . . . . . . . . . . . . . . . . . . . . . . 290IV Topics 29229 Mixing Overloading with Inheritance 29429.1 What is the difference between overloaded functions and overriddenfunctions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29429.2 What is the hiding rule? . . . . . . . . . . . . . . . . . . . . . . . 29529.3 How should the hiding rule be handled? . . . . . . . . . . . . . . 29829.4 What should a derived class do when it redefines some but not allof a set of overloaded member functions inherited from the baseclass? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30029.5 Can virtual functions be overloaded? . . . . . . . . . . . . . . . . 30130 The Big Three 30430.1 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 30430.2 What are the Big Three? . . . . . . . . . . . . . . . . . . . . . . 30430.3 What happens when an object is destroyed that doesn’t have anexplicit destructor? . . . . . . . . . . . . . . . . . . . . . . . . . . 30530.4 What happens if an object is copied but doesn’t have an explicitcopy constructor? . . . . . . . . . . . . . . . . . . . . . . . . . . . 30630.5 What happens when an object that doesn’t have an explicit assignmentoperator is assigned? . . . . . . . . . . . . . . . . . . . 30730.6 What is the Law of the Big Three? . . . . . . . . . . . . . . . . . 30830.7 Which of the Big Three usually shows up first? . . . . . . . . . . 30830.8 What is remote ownership? . . . . . . . . . . . . . . . . . . . . . 30930.9 How is remote ownership special? . . . . . . . . . . . . . . . . . . 31030.10What if a class owns a referent and doesn’t have all of the BigThree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31030.11Are there any C++ classes that help manage remote ownership? 31230.12Does auto ptr enforce the Law of the Big Three and solve theproblems associated with remote ownership? . . . . . . . . . . . . 314xiii30.13Are there cases where one or two of the Big Three may be neededbut not all three? . . . . . . . . . . . . . . . . . . . . . . . . . . . 31630.14Are there any other circumstances that might explicitly warrantthe Big Three? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31730.15Why does copying an object using memcpy() cause a programcrash? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31730.16Why do programs with variable-length argument lists crash? . . 31830.17Why do programs that use realloc() to reallocate an array ofobjects crash? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32031 Using Objects to Prevent Memory Leaks 32131.1 When are memory leaks important? . . . . . . . . . . . . . . . . 32131.2 What is the easiest way to avoid memory leaks? . . . . . . . . . . 32131.3 What are the most important principles for resource management?32431.4 Should the object that manages a resource also perform operationsthat may throw exceptions? . . . . . . . . . . . . . . . . . . 32531.5 Should an object manage two or more resources? . . . . . . . . . 32631.6 What if an object has a pointer to an allocation and one of theobject’s member functions deletes the allocation? . . . . . . . . 32731.7 How should a pointer variable be handled after being passed todelete? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32831.8 What should be done with a pointer to an object that is allocatedand deallocated in the same scope? . . . . . . . . . . . . . . . . . 32831.9 How easy is it to implement reference counting with pointer semantics?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32931.10Is reference counting with copy-on-write semantics hard to implement?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33131.11How can reference counting be implemented with copy-on-writesemantics for a hierarchy of classes? . . . . . . . . . . . . . . . . 33332 Wild Pointers and Other Devilish Errors 33732.1 What is a wild pointer? . . . . . . . . . . . . . . . . . . . . . . . 33732.2 What happens to a program that has even one wild pointer? . . 33732.3 What does the compiler mean by the warning “Returning a referenceto a local object”? . . . . . . . . . . . . . . . . . . . . . . 33832.4 How should pointers across block boundaries be controlled? . . . 33832.5 Is the reference-versus-pointer issue influenced by whether or notthe object is allocated from the heap? . . . . . . . . . . . . . . . 33932.6 When should C-style pointer casts be used? . . . . . . . . . . . . 34032.7 Is it safe to bind a reference variable to a temporary object? . . . 34132.8 Should a parameter passed by const reference be returned byconst reference? . . . . . . . . . . . . . . . . . . . . . . . . . . . 34132.9 Should template functions for things like min(x,y) or abs(x)return a const reference? . . . . . . . . . . . . . . . . . . . . . . 34232.10When is zero not necessarily zero? . . . . . . . . . . . . . . . . . 34233 High-Performance Software 34533.1 Is bad performance a result of bad design or bad coding? . . . . 34533.2 What are some techniques for improving performance? . . . . . . 34633.3 What is an advantage of using pointers and references? . . . . . . 347xiv33.4 What is a disadvantage of lots of references and pointers? . . . . 34933.5 How else can member objects improve performance over pointers? 35133.6 Which is better, ++i or i++? . . . . . . . . . . . . . . . . . . . . 35133.7 What is the performance difference between Fred x(5); andFred y = 5; and Fred z = Fred(5);? . . . . . . . . . . . . . . 35333.8 What kinds of applications should consider using final classes andfinal member functions? . . . . . . . . . . . . . . . . . . . . . . . 35433.9 What is a final class? . . . . . . . . . . . . . . . . . . . . . . . . . 35433.10What is a final member function? . . . . . . . . . . . . . . . . . . 35533.11How can final classes and final member functions improve performance?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35633.12When should a nonfinal virtual function be invoked with a fullyqualified name? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35733.13Should full qualification be used when calling another memberfunction of the same class? . . . . . . . . . . . . . . . . . . . . . 35833.14Do final classes and final member functions cause a lot of codeduplication? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35933.15Why do some developers dislike final member functions and finalclasses? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36133.16Can a programming language — rather than just the compiler —affect the performance of software? . . . . . . . . . . . . . . . . . 36134 COM and ActiveX 36234.1 Who should read this chapter? . . . . . . . . . . . . . . . . . . . 36234.2 What is the Component Object Model? . . . . . . . . . . . . . . 36234.3 What are ActiveX and OLE? . . . . . . . . . . . . . . . . . . . . 36334.4 What does the name Component Object Model mean? . . . . . . 36434.5 What is a “binary object model”? . . . . . . . . . . . . . . . . . 36434.6 What are the key features of COM? . . . . . . . . . . . . . . . . 36534.7 What are GUIDs? . . . . . . . . . . . . . . . . . . . . . . . . . . 36534.8 Why does COM need GUIDs (and CLSIDs and IIDs)? . . . . . . 36734.9 What is an interface? . . . . . . . . . . . . . . . . . . . . . . . . . 36734.10What is the IUnknown interface? . . . . . . . . . . . . . . . . . . 36934.11How many ways are there to specify COM interfaces? . . . . . . 37034.12What are COM classes and COM objects? . . . . . . . . . . . . . 37234.13How hard is it for callers to create and use a COM object? . . . 37634.14How does COM provide language transparency? . . . . . . . . . 37834.15How does COM provide location transparency? . . . . . . . . . . 37934.16What types of errors occur due to reference counting? . . . . . . 38034.17What mechanism does COM define for error handling? . . . . . . 38134.18How are interfaces versioned? . . . . . . . . . . . . . . . . . . . . 38134.19Is COM object oriented? . . . . . . . . . . . . . . . . . . . . . . . 38234.20What is the biggest problem with COM? . . . . . . . . . . . . . 38334.21What are the major differences between COM and C++? . . . . 38434.22What should a class be defined as a COM class? . . . . . . . . . 38634.23What is Automation? . . . . . . . . . . . . . . . . . . . . . . . . 38834.24What are dispatch interfaces? . . . . . . . . . . . . . . . . . . . . 38834.25When should a class expose a Dispatch interface? . . . . . . . . . 38934.26How does Automation work? . . . . . . . . . . . . . . . . . . . . 39034.27How does Invoke accomplish all of this? . . . . . . . . . . . . . . 392xv34.28What is a type library? . . . . . . . . . . . . . . . . . . . . . . . 39234.29What are the benefits of using type libraries? . . . . . . . . . . . 39334.30How do type libraries improve performance? . . . . . . . . . . . . 39434.31What are dual interfaces? . . . . . . . . . . . . . . . . . . . . . . 39434.32What limitations are there on dual interfaces? . . . . . . . . . . . 39534.33What are OLE custom controls and ActiveX controls? . . . . . . 39634.34Why do ActiveX controls differ from OLE custom controls? . . . 39734.35What is a control container? . . . . . . . . . . . . . . . . . . . . . 39834.36What are component categories? . . . . . . . . . . . . . . . . . . 39934.37What are events? . . . . . . . . . . . . . . . . . . . . . . . . . . . 39934.38What is DCOM? . . . . . . . . . . . . . . . . . . . . . . . . . . . 40034.39How stable is DCOM’s infrastructure? . . . . . . . . . . . . . . . 40134.40What is COM+? . . . . . . . . . . . . . . . . . . . . . . . . . . . 40135 Transitioning to CORBA 40335.1 What is CORBA? . . . . . . . . . . . . . . . . . . . . . . . . . . 40335.2 What is an ORB? . . . . . . . . . . . . . . . . . . . . . . . . . . 40335.3 What is IDL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40335.4 What is COS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40435.5 What is OMA? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40435.6 What is OMG? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40435.7 What is the purpose of this chapter? . . . . . . . . . . . . . . . . 40435.8 What is the most important message of this chapter? . . . . . . . 40535.9 What are the important concepts behind CORBA? . . . . . . . . 40535.10Isn’t OMG IDL pretty much the same as C++? . . . . . . . . . . 40635.11Is the life cycle of a CORBA object the same as the life cycle ofa C++ object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40735.12Is the C++ code that interacts with the CORBA implementationportable to a different CORBA vendor? . . . . . . . . . . . . . . 40835.13How do CORBA exceptions compare to C++ exceptions? . . . . 40835.14Which CORBA implementation is best? Is CORBA better thanCOM? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40936 C Language Considerations 41036.1 What are the main issues when mixing C and C++ code in thesame application? . . . . . . . . . . . . . . . . . . . . . . . . . . . 41036.2 How can C++ code call C code? . . . . . . . . . . . . . . . . . . 41036.3 How can C code call C++ code? . . . . . . . . . . . . . . . . . . 41136.4 Why is the linker giving errors for C functions called from C+functions and vice versa? . . . . . . . . . . . . . . . . . . . . . . 41236.5 How can an object of a C++ class be passed to or from a Cfunction? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41336.6 Can a C function directly access data in an object of a C++ class?41436.7 Can C++ I/O (<iostream>) be mixed with C I/O (<stdio.h)? 41536.8 Which is safer: <iostream> or <stdio.h>? . . . . . . . . . . . . 41636.9 Which is more extensible: <iostream> or <stdio.h>? . . . . . . 41636.10Which is more flexible: <iostream> or <stdio.h>? . . . . . . . . 41636.11Why does it seem that C++ programming feels farther away fromthe machine than C? . . . . . . . . . . . . . . . . . . . . . . . . . 41736.12Why does C++ do more things behind your back than C does? . 417xvi37 Private and Protected Inheritance 41837.1 What are private inheritance and protected inheritance? . . . . . 41837.2 What is the difference between private inheritance and protectedinheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41937.3 What is the syntax and semantics for private and protected inheritance?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41937.4 When should normal has-a be used, rather than private or protectedinheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . 42137.5 What are the access rules for public, protected, and private inheritance?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42137.6 In a private or protected derived class, how can a member functionthat was public in the base class be made public in thederived class? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42237.7 Should a pointer be cast from a private or protected derived classto its base class? . . . . . . . . . . . . . . . . . . . . . . . . . . . 42338 Pointers to Member Functions 42438.1 What is the type of a pointer to a nonstatic member function? . 42438.2 Can pointers to nonstatic member functions be passed to signalhandlers, X event call-back handlers, and so on, that expect Clikefunction pointers? . . . . . . . . . . . . . . . . . . . . . . . . 42538.3 What is one of the most common errors when using pointers tomember functions? . . . . . . . . . . . . . . . . . . . . . . . . . . 42638.4 How is an array of pointers to nonstatic member functions declared?42739 The Transition to OO and C++ 42939.1 Who should read this chapter? . . . . . . . . . . . . . . . . . . . 42939.2 What are the key messages of this chapter? . . . . . . . . . . . . 42939.3 How many stages of technical expertise are there in the transition?43039.4 Why bother defining levels of expertise? . . . . . . . . . . . . . . 43039.5 Can OO/C++ be learned from a book? . . . . . . . . . . . . . . 43139.6 Can OO/C++ be learned from a course? . . . . . . . . . . . . . 43139.7 What is the key to learning OO/C++? . . . . . . . . . . . . . . 43239.8 Are small projects a good way to get your feet wet? . . . . . . . 43239.9 Does being a good C programmer help when learning OO/C++? 43339.10What skills help when learning OO/C++? . . . . . . . . . . . . . 434xviiDownload:http://www.megaupload.com/?d=L1JWUMNOhttp://www.mediafire.com/?5r7o38pmm47s3m0http://www.multiupload.com/O42Y08NUVFhttp://www.fileshare.ro/28930635804.8http://www.girlshare.ro/2645981.8 Quote