home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / C++-faq / part7 < prev    next >
Internet Message Format  |  2000-03-01  |  55KB

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!news-out.cwix.com!newsfeed.cwix.com!newsfeed.direct.ca!novia!nntp3.cerf.net!nntp2.cerf.net!news.cerf.net!not-for-mail
  2. From: mpcline@nic.cerf.net (Marshall Cline)
  3. Newsgroups: comp.lang.c++,comp.answers,news.answers,alt.comp.lang.learn.c-c++
  4. Subject: C++ FAQ (part 7 of 10)
  5. Followup-To: comp.lang.c++
  6. Date: 29 Feb 2000 20:07:07 GMT
  7. Organization: ATT Cerfnet
  8. Lines: 1203
  9. Approved: news-answers-request@mit.edu
  10. Distribution: world
  11. Expires: +1 month
  12. Message-ID: <89h8tb$bu2$1@news.cerf.net>
  13. Reply-To: cline@parashift.com (Marshall Cline)
  14. NNTP-Posting-Host: nic1.san.cerf.net
  15. X-Trace: news.cerf.net 951854827 12226 192.215.81.88 (29 Feb 2000 20:07:07 GMT)
  16. X-Complaints-To: abuse@cerf.net
  17. NNTP-Posting-Date: 29 Feb 2000 20:07:07 GMT
  18. Summary: Please read this before posting to comp.lang.c++
  19. Xref: senator-bedfellow.mit.edu comp.lang.c++:453820 comp.answers:39860 news.answers:178219 alt.comp.lang.learn.c-c++:40798
  20.  
  21. Archive-name: C++-faq/part7
  22. Posting-Frequency: monthly
  23. Last-modified: Feb 29, 2000
  24. URL: http://marshall-cline.home.att.net/cpp-faq-lite/
  25.  
  26. AUTHOR: Marshall Cline / cline@parashift.com / 972-931-9470
  27.  
  28. COPYRIGHT: This posting is part of "C++ FAQ Lite."  The entire "C++ FAQ Lite"
  29. document is Copyright(C)1991-2000 Marshall Cline, Ph.D., cline@parashift.com.
  30. All rights reserved.  Copying is permitted only under designated situations.
  31. For details, see section [1].
  32.  
  33. NO WARRANTY: THIS WORK IS PROVIDED ON AN "AS IS" BASIS.  THE AUTHOR PROVIDES NO
  34. WARRANTY WHATSOEVER, EITHER EXPRESS OR IMPLIED, REGARDING THE WORK, INCLUDING
  35. WARRANTIES WITH RESPECT TO ITS MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR
  36. PURPOSE.
  37.  
  38. C++-FAQ-Lite != C++-FAQ-Book: This document, C++ FAQ Lite, is not the same as
  39. the C++ FAQ Book.  The book (C++ FAQs, Cline and Lomow, Addison-Wesley) is 500%
  40. larger than this document, and is available in bookstores.  For details, see
  41. section [3].
  42.  
  43. ==============================================================================
  44.  
  45. SECTION [18]: Const correctness
  46.  
  47.  
  48. [18.1] What is "const correctness"?
  49.  
  50. A good thing.  It means using the keyword const to prevent const objects from
  51. getting mutated.
  52.  
  53. For example, if you wanted to create a function f() that accepted a String,
  54. plus you want to promise callers not to change the caller's String that gets
  55. passed to f(), you can have f() receive its String parameter...
  56.  * void f1(const String& s);      // Pass by reference-to-const
  57.  * void f2(const String* sptr);   // Pass by pointer-to-const
  58.  * void f3(String s);             // Pass by value
  59.  
  60. In the pass by reference-to-const and pass by pointer-to-const cases, any
  61. attempts to change to the caller's String within the f() functions would be
  62. flagged by the compiler as an error at compile-time.  This check is done
  63. entirely at compile-time: there is no run-time space or speed cost for the
  64. const.  In the pass by value case (f3()), the called function gets a copy of
  65. the caller's String.  This means that f3() can change its local copy, but the
  66. copy is destroyed when f3() returns.  In particular f3() cannot change the
  67. caller's String object.
  68.  
  69. As an opposite example, if you wanted to create a function g() that accepted a
  70. String, but you want to let callers know that g() might change the caller's
  71. String object.  In this case you can have g() receive its String parameter...
  72.  * void g1(String& s);      // Pass by reference-to-non-const
  73.  * void g2(String* sptr);   // Pass by pointer-to-non-const
  74.  
  75. The lack of const in these functions tells the compiler that they are allowed
  76. to (but are not required to) change the caller's String object.  Thus they can
  77. pass their String to any of the f() functions, but only f3() (the one that
  78. receives its parameter "by value") can pass its String to g1() or g2().  If
  79. f1() or f2() need to call either g() function, a local copy of the String
  80. object must be passed to the g() function; the parameter to f1() or f2() cannot
  81. be directly passed to either g() function.  E.g.,
  82.  
  83.     void g1(String& s);
  84.  
  85.     void f1(const String& s)
  86.     {
  87.       g1(s);          // Compile-time Error since s is const
  88.  
  89.       String localCopy = s;
  90.       g1(localCopy);  // OK since localCopy is not const
  91.     }
  92.  
  93. Naturally in the above case, any changes that g1() makes are made to the
  94. localCopy object that is local to f1().  In particular, no changes will be made
  95. to the const parameter that was passed by reference to f1().
  96.  
  97. ==============================================================================
  98.  
  99. [18.2] How is "const correctness" related to ordinary type safety?
  100.  
  101. Declaring the const-ness of a parameter is just another form of type safety.
  102. It is almost as if a const String, for example, is a different class than an
  103. ordinary String, since the const variant is missing the various mutative
  104. operations in the non-const variant (e.g., you can imagine that a const String
  105. simply doesn't have an assignment operator).
  106.  
  107. If you find ordinary type safety helps you get systems correct (it does;
  108. especially in large systems), you'll find const correctness helps also.
  109.  
  110. ==============================================================================
  111.  
  112. [18.3] Should I try to get things const correct "sooner" or "later"?
  113.  
  114. At the very, very, very beginning.
  115.  
  116. Back-patching const correctness results in a snowball effect: every const you
  117. add "over here" requires four more to be added "over there."
  118.  
  119. ==============================================================================
  120.  
  121. [18.4] What does "const Fred* p" mean?
  122.  
  123. It means p points to an object of class Fred, but p can't be used to change
  124. that Fred object (naturally p could also be NULL).
  125.  
  126. For example, if class Fred has a const member function[18.9] called inspect(),
  127. saying p->inspect() is OK.  But if class Fred has a non-const member
  128. function[18.9] called mutate(), saying p->mutate() is an error (the error is
  129. caught by the compiler; no run-time tests are done, which means const doesn't
  130. slow your program down).
  131.  
  132. ==============================================================================
  133.  
  134. [18.5] What's the difference between "const Fred* p", "Fred* const p" and
  135.        "const Fred* const p"?
  136.  
  137. You have to read pointer declarations right-to-left.
  138.  * const Fred* p means "p points to a Fred that is const" -- that is, the Fred
  139.    object can't be changed via p[18.13].
  140.  * Fred* const p means "p is a const pointer to a Fred" -- that is, you can
  141.    change the Fred object via p[18.13], but you can't change the pointer p
  142.    itself.
  143.  * const Fred* const p means "p is a const pointer to a const Fred" -- that is,
  144.    you can't change the pointer p itself, nor can you change the Fred object
  145.    via p[18.13].
  146.  
  147. ==============================================================================
  148.  
  149. [18.6] What does "const Fred& x" mean?
  150.  
  151. It means x aliases a Fred object, but x can't be used to change that Fred
  152. object.
  153.  
  154. For example, if class Fred has a const member function[18.9] called inspect(),
  155. saying x.inspect() is OK.  But if class Fred has a non-const member
  156. function[18.9] called mutate(), saying x.mutate() is an error (the error is
  157. caught by the compiler; no run-time tests are done, which means const doesn't
  158. slow your program down).
  159.  
  160. ==============================================================================
  161.  
  162. [18.7] Does "Fred& const x" make any sense?
  163.  
  164. No, it is nonsense.
  165.  
  166. To find out what the above declaration means, you have to read it
  167. right-to-left[18.5].  Thus "Fred& const x" means "x is a const reference to a
  168. Fred".  But that is redundant, since references are always const.  You can't
  169. reseat a reference[8.4].  Never.  With or without the const.
  170.  
  171. In other words, "Fred& const x" is functionally equivalent to "Fred& x".  Since
  172. you're gaining nothing by adding the const after the &, you shouldn't add it
  173. since it will confuse people.  I.e., the const will make some people think that
  174. the Fred is const, as if you had said "const Fred& x".
  175.  
  176. ==============================================================================
  177.  
  178. [18.8] What does "Fred const& x" mean?
  179.  
  180. "Fred const& x" is functionally equivalent to "const Fred& x"[18.6].
  181.  
  182. The problem with using "Fred const& x" (with the const before the &) is that it
  183. could easily be mis-typed as the nonsensical "Fred &const x"[18.7] (with the
  184. const after the &).
  185.  
  186. Better to simply use const Fred& x.
  187.  
  188. ==============================================================================
  189.  
  190. [18.9] What is a "const member function"?
  191.  
  192. A member function that inspects (rather than mutates) its object.
  193.  
  194. A const member function is indicated by a const suffix just after the member
  195. function's parameter list.  Member functions with a const suffix are called
  196. "const member functions" or "inspectors." Member functions without a const
  197. suffix are called "non-const member functions" or "mutators."
  198.  
  199.     class Fred {
  200.     public:
  201.       void inspect() const;   // This member promises NOT to change *this
  202.       void mutate();          // This member function might change *this
  203.     };
  204.  
  205.     void userCode(Fred& changeable, const Fred& unchangeable)
  206.     {
  207.       changeable.inspect();   // OK: doesn't change a changeable object
  208.       changeable.mutate();    // OK: changes a changeable object
  209.  
  210.       unchangeable.inspect(); // OK: doesn't change an unchangeable object
  211.       unchangeable.mutate();  // ERROR: attempt to change unchangeable object
  212.     }
  213.  
  214. The error in unchangeable.mutate() is caught at compile time.  There is no
  215. runtime space or speed penalty for const.
  216.  
  217. The trailing const on inspect() member function means that the abstract
  218. (client-visible) state of the object isn't going to change.  This is slightly
  219. different from promising that the "raw bits" of the object's struct aren't
  220. going to change.  C++ compilers aren't allowed to take the "bitwise"
  221. interpretation unless they can solve the aliasing problem, which normally can't
  222. be solved (i.e., a non-const alias could exist which could modify the state of
  223. the object).  Another (important) insight from this aliasing issue: pointing at
  224. an object with a pointer-to-const doesn't guarantee that the object won't
  225. change; it promises only that the object won't change via that pointer.
  226.  
  227. ==============================================================================
  228.  
  229. [18.10] What do I do if I want to update an "invisible" data member inside a
  230.         const member function? [UPDATED!]
  231.  
  232. [Recently added a warning against use of const_cast on const objects thanks to
  233. TiTi (on 3/00).]
  234.  
  235. Use mutable (or, as a last resort, use const_cast).
  236.  
  237. A small percentage of inspectors need to make innocuous changes to data members
  238. (e.g., a Set object might want to cache its last lookup in hopes of improving
  239. the performance of its next lookup).  By saying the changes are "innocuous," I
  240. mean that the changes wouldn't be visible from outside the object's interface
  241. (otherwise the member function would be a mutator rather than an inspector).
  242.  
  243. When this happens, the data member which will be modified should be marked as
  244. mutable (put the mutable keyword just before the data member's declaration;
  245. i.e., in the same place where you could put const).  This tells the compiler
  246. that the data member is allowed to change during a const member function.  If
  247. your compiler doesn't support the mutable keyword, you can cast away the
  248. const'ness of this via the const_cast keyword (but see the NOTE below before
  249. doing this).  E.g., in Set::lookup() const, you might say,
  250.  
  251.     Set* self = const_cast<Set*>(this);
  252.       // See the NOTE below before doing this!
  253.  
  254. After this line, self will have the same bits as this (e.g., self == this), but
  255. self is a Set* rather than a const Set*.  Therefore you can use self to modify
  256. the object pointed to by this.
  257.  
  258. NOTE: there is an extremely unlikely error that can occur with const_cast.  It
  259. only happens when three very rare things are combined at the same time: a data
  260. member that ought to be mutable (such as is discussed above), a compiler that
  261. doesn't support the mutable keyword, and an object that was originally defined
  262. to be const (as opposed to a normal, non-const object that is pointed to by a
  263. pointer-to-const).  Although this combination is so rare that it may never
  264. happen to you, if it ever did happen the code may not work (the Standard says
  265. the behavior is undefined).
  266.  
  267. If you ever want to use const_cast, use mutable instead.  In other words, if
  268. you ever need to change a member of an object, and that object is pointed to by
  269. a pointer-to-const, the safest and simplest thing to do is add mutable to the
  270. member's declaration.  You can use const_cast if you are sure that the actual
  271. object isn't const (e.g., if you are sure the object is declared something like
  272. this: Set s;), but if the object itself might be const (e.g., if it might be
  273. declared like: const Set s;), use mutable rather than const_cast.
  274.  
  275. Please don't write and tell me that version X of compiler Y on machine Z allows
  276. you to change a non-mutable member of a const object.  I don't care -- it is
  277. illegal according to the language and your code will probably fail on a
  278. different compiler or even a different version (an upgrade) of the same
  279. compiler.  Just say no.  Use mutable instead.
  280.  
  281. ==============================================================================
  282.  
  283. [18.11] Does const_cast mean lost optimization opportunities?
  284.  
  285. In theory, yes; in practice, no.
  286.  
  287. Even if the language outlawed const_cast, the only way to avoid flushing the
  288. register cache across a const member function call would be to solve the
  289. aliasing problem (i.e., to prove that there are no non-const pointers that
  290. point to the object).  This can happen only in rare cases (when the object is
  291. constructed in the scope of the const member function invocation, and when all
  292. the non-const member function invocations between the object's construction and
  293. the const member function invocation are statically bound, and when every one
  294. of these invocations is also inlined, and when the constructor itself is
  295. inlined, and when any member functions the constructor calls are inline).
  296.  
  297. ==============================================================================
  298.  
  299. [18.12] Why does the compiler allow me to change an int after I've pointed at
  300.         it with a const int*?
  301.  
  302. Because "const int* p" means "p promises not to change the *p," not "*p
  303. promises not to change."
  304.  
  305. Causing a const int* to point to an int doesn't const-ify the int.  The int
  306. can't be changed via the const int*, but if someone else has an int* (note: no
  307. const) that points to ("aliases") the same int, then that int* can be used to
  308. change the int.  For example:
  309.  
  310.     void f(const int* p1, int* p2)
  311.     {
  312.       int i = *p1;         // Get the (original) value of *p1
  313.       *p2 = 7;             // If p1 == p2, this will also change *p1
  314.       int j = *p1;         // Get the (possibly new) value of *p1
  315.       if (i != j) {
  316.         cout << "*p1 changed, but it didn't change via pointer p1!\n";
  317.         assert(p1 == p2);  // This is the only way *p1 could be different
  318.       }
  319.     }
  320.  
  321.     int main()
  322.     {
  323.       int x;
  324.       f(&x, &x);           // This is perfectly legal (and even moral!)
  325.     }
  326.  
  327. Note that main() and f(const int*,int*) could be in different compilation units
  328. that are compiled on different days of the week.  In that case there is no way
  329. the compiler can possibly detect the aliasing at compile time.  Therefore there
  330. is no way we could make a language rule that prohibits this sort of thing.  In
  331. fact, we wouldn't even want to make such a rule, since in general it's
  332. considered a feature that you can have many pointers pointing to the same
  333. thing.  The fact that one of those pointers promises not to change the
  334. underlying "thing" is just a promise made by the pointer; it's not a promise
  335. made by the "thing".
  336.  
  337. ==============================================================================
  338.  
  339. [18.13] Does "const Fred* p" mean that *p can't change? [UPDATED!]
  340.  
  341. [Recently added an indication that there might be other non-const ways to get
  342. at the object thanks to Stan Brown (on 1/00).]
  343.  
  344. No! (This is related to the FAQ about aliasing of int pointers[18.12].)
  345.  
  346. "const Fred* p" means that the Fred can't be changed via pointer p, but there
  347. might be other ways to get at the object without going through a const (such as
  348. an aliased non-const pointer such as a Fred*).  For example, if you have two
  349. pointers "const Fred* p" and "Fred* q" that point to the same Fred object
  350. (aliasing), pointer q can be used to change the Fred object but pointer p
  351. cannot.
  352.  
  353.     class Fred {
  354.     public:
  355.       void inspect() const;   // A const member function[18.9]
  356.       void mutate();          // A non-const member function[18.9]
  357.     };
  358.  
  359.     int main()
  360.     {
  361.       Fred f;
  362.       const Fred* p = &f;
  363.             Fred* q = &f;
  364.  
  365.       p->inspect();    // OK: No change to *p
  366.       p->mutate();     // Error: Can't change *p via p
  367.  
  368.       q->inspect();    // OK: q is allowed to inspect the object
  369.       q->mutate();     // OK: q is allowed to mutate the object
  370.  
  371.       f.inspect();     // OK: f is allowed to inspect the object
  372.       f.mutate();      // OK: f is allowed to mutate the object
  373.     }
  374.  
  375. ==============================================================================
  376.  
  377. SECTION [19]: Inheritance -- basics
  378.  
  379.  
  380. [19.1] Is inheritance important to C++?
  381.  
  382. Yep.
  383.  
  384. Inheritance is what separates abstract data type (ADT) programming from OO
  385. programming.
  386.  
  387. ==============================================================================
  388.  
  389. [19.2] When would I use inheritance?
  390.  
  391. As a specification device.
  392.  
  393. Human beings abstract things on two dimensions: part-of and kind-of.  A Ford
  394. Taurus is-a-kind-of-a Car, and a Ford Taurus has-a Engine, Tires, etc.  The
  395. part-of hierarchy has been a part of software since the ADT style became
  396. relevant; inheritance adds "the other" major dimension of decomposition.
  397.  
  398. ==============================================================================
  399.  
  400. [19.3] How do you express inheritance in C++?
  401.  
  402. By the : public syntax:
  403.  
  404.     class Car : public Vehicle {
  405.     public:
  406.       // ...
  407.     };
  408.  
  409. We state the above relationship in several ways:
  410.  * Car is "a kind of a" Vehicle
  411.  * Car is "derived from" Vehicle
  412.  * Car is "a specialized" Vehicle
  413.  * Car is the "subclass" of Vehicle
  414.  * Vehicle is the "base class" of Car
  415.  * Vehicle is the "superclass" of Car (this not as common in the C++ community)
  416.  
  417. (Note: this FAQ has to do with public inheritance; private and protected
  418. inheritance[24] are different.)
  419.  
  420. ==============================================================================
  421.  
  422. [19.4] Is it OK to convert a pointer from a derived class to its base class?
  423.  
  424. Yes.
  425.  
  426. An object of a derived class is a kind of the base class.  Therefore the
  427. conversion from a derived class pointer to a base class pointer is perfectly
  428. safe, and happens all the time.  For example, if I am pointing at a car, I am
  429. in fact pointing at a vehicle, so converting a Car* to a Vehicle* is perfectly
  430. safe and normal:
  431.  
  432.     void f(Vehicle* v);
  433.     void g(Car* c) { f(c); }  // Perfectly safe; no cast
  434.  
  435. (Note: this FAQ has to do with public inheritance; private and protected
  436. inheritance[24] are different.)
  437.  
  438. ==============================================================================
  439.  
  440. [19.5] What's the difference between public:, private:, and protected:?
  441.  
  442.  * A member (either data member or member function) declared in a private:
  443.    section of a class can only be accessed by member functions and friends[14]
  444.    of that class
  445.  * A member (either data member or member function) declared in a protected:
  446.    section of a class can only be accessed by member functions and friends[14]
  447.    of that class, and by member functions and friends[14] of derived classes
  448.  * A member (either data member or member function) declared in a public:
  449.    section of a class can be accessed by anyone
  450.  
  451. ==============================================================================
  452.  
  453. [19.6] Why can't my derived class access private: things from my base class?
  454.  
  455. To protect you from future changes to the base class.
  456.  
  457. Derived classes do not get access to private members of a base class.  This
  458. effectively "seals off" the derived class from any changes made to the private
  459. members of the base class.
  460.  
  461. ==============================================================================
  462.  
  463. [19.7] How can I protect subclasses from breaking when I change internal parts?
  464.  
  465. A class has two distinct interfaces for two distinct sets of clients:
  466.  * It has a public: interface that serves unrelated classes
  467.  * It has a protected: interface that serves derived classes
  468.  
  469. Unless you expect all your subclasses to be built by your own team, you should
  470. consider making your base class's bits be private:, and use protected: inline
  471. access functions by which derived classes will access the private data in the
  472. base class.  This way the private bits can change, but the derived class's code
  473. won't break unless you change the protected access functions.
  474.  
  475. ==============================================================================
  476.  
  477. SECTION [20]: Inheritance -- virtual functions
  478.  
  479.  
  480. [20.1] What is a "virtual member function"?
  481.  
  482. From an OO perspective, it is the single most important feature of C++: [6.8],
  483. [6.9].
  484.  
  485. A virtual function allows derived classes to replace the implementation
  486. provided by the base class.  The compiler makes sure the replacement is always
  487. called whenever the object in question is actually of the derived class, even
  488. if the object is accessed by a base pointer rather than a derived pointer.
  489. This allows algorithms in the base class to be replaced in the derived class,
  490. even if users don't know about the derived class.
  491.  
  492. The derived class can either fully replace ("override") the base class member
  493. function, or the derived class can partially replace ("augment") the base class
  494. member function.  The latter is accomplished by having the derived class member
  495. function call the base class member function, if desired.
  496.  
  497. ==============================================================================
  498.  
  499. [20.2] How can C++ achieve dynamic binding yet also static typing? [UPDATED!]
  500.  
  501. [Recently added the definition of polymorphism thanks to Kemberli Jennings (on
  502. 1/00).]
  503.  
  504. When you have a pointer to an object, the object may actually be of a class
  505. that is derived from the class of the pointer (e.g., a Vehicle* that is
  506. actually pointing to a Car object; this is called "polymorphism").  Thus there
  507. are two types: the (static) type of the pointer (Vehicle, in this case), and
  508. the (dynamic) type of the pointed-to object (Car, in this case).
  509.  
  510. Static typing means that the legality of a member function invocation is
  511. checked at the earliest possible moment: by the compiler at compile time.  The
  512. compiler uses the static type of the pointer to determine whether the member
  513. function invocation is legal.  If the type of the pointer can handle the member
  514. function, certainly the pointed-to object can handle it as well.  E.g., if
  515. Vehicle has a certain member function, certainly Car also has that member
  516. function since Car is a kind-of Vehicle.
  517.  
  518. Dynamic binding means that the address of the code in a member function
  519. invocation is determined at the last possible moment: based on the dynamic type
  520. of the object at run time.  It is called "dynamic binding" because the binding
  521. to the code that actually gets called is accomplished dynamically (at run
  522. time).  Dynamic binding is a result of virtual functions.
  523.  
  524. ==============================================================================
  525.  
  526. [20.3] What's the difference between how virtual and non-virtual member
  527.        functions are called?
  528.  
  529. Non-virtual member functions are resolved statically.  That is, the member
  530. function is selected statically (at compile-time) based on the type of the
  531. pointer (or reference) to the object.
  532.  
  533. In contrast, virtual member functions are resolved dynamically (at run-time).
  534. That is, the member function is selected dynamically (at run-time) based on the
  535. type of the object, not the type of the pointer/reference to that object.  This
  536. is called "dynamic binding." Most compilers use some variant of the following
  537. technique: if the object has one or more virtual functions, the compiler puts a
  538. hidden pointer in the object called a "virtual-pointer" or "v-pointer." This
  539. v-pointer points to a global table called the "virtual-table" or "v-table."
  540.  
  541. The compiler creates a v-table for each class that has at least one virtual
  542. function.  For example, if class Circle has virtual functions for draw() and
  543. move() and resize(), there would be exactly one v-table associated with class
  544. Circle, even if there were a gazillion Circle objects, and the v-pointer of
  545. each of those Circle objects would point to the Circle v-table.  The v-table
  546. itself has pointers to each of the virtual functions in the class.  For
  547. example, the Circle v-table would have three pointers: a pointer to
  548. Circle::draw(), a pointer to Circle::move(), and a pointer to Circle::resize().
  549.  
  550. During a dispatch of a virtual function, the run-time system follows the
  551. object's v-pointer to the class's v-table, then follows the appropriate slot in
  552. the v-table to the method code.
  553.  
  554. The space-cost overhead of the above technique is nominal: an extra pointer per
  555. object (but only for objects that will need to do dynamic binding), plus an
  556. extra pointer per method (but only for virtual methods).  The time-cost
  557. overhead is also fairly nominal: compared to a normal function call, a virtual
  558. function call requires two extra fetches (one to get the value of the
  559. v-pointer, a second to get the address of the method).  None of this runtime
  560. activity happens with non-virtual functions, since the compiler resolves
  561. non-virtual functions exclusively at compile-time based on the type of the
  562. pointer.
  563.  
  564. Note: the above discussion is simplified considerably, since it doesn't account
  565. for extra structural things like multiple inheritance, virtual inheritance,
  566. RTTI, etc., nor does it account for space/speed issues such as page faults,
  567. calling a function via a pointer-to-function, etc.  If you want to know about
  568. those other things, please ask comp.lang.c++; PLEASE DO NOT SEND E-MAIL TO ME!
  569.  
  570. ==============================================================================
  571.  
  572. [20.4] When should my destructor be virtual?
  573.  
  574. When you may delete a derived object via a base pointer.
  575.  
  576. virtual functions bind to the code associated with the class of the object,
  577. rather than with the class of the pointer/reference.  When you say
  578. delete basePtr, and the base class has a virtual destructor, the destructor
  579. that gets invoked is the one associated with the type of the object *basePtr,
  580. rather than the one associated with the type of the pointer.  This is generally
  581. A Good Thing.
  582.  
  583. TECHNO-GEEK WARNING; PUT YOUR PROPELLER HAT ON.
  584. Technically speaking, you need a base class's destructor to be virtual if and
  585. only if you intend to allow someone to invoke an object's destructor via a base
  586. class pointer (this is normally done implicitly via delete), and the object
  587. being destructed is of a derived class that has a non-trivial destructor.  A
  588. class has a non-trivial destructor if it either has an explicitly defined
  589. destructor, or if it has a member object or a base class that has a non-trivial
  590. destructor (note that this is a recursive definition (e.g., a class has a
  591. non-trivial destructor if it has a member object (which has a base class (which
  592. has a member object (which has a base class (which has an explicitly defined
  593. destructor)))))).
  594. END TECHNO-GEEK WARNING; REMOVE YOUR PROPELLER HAT
  595.  
  596. If you had a hard grokking the previous rule, try this (over)simplified one on
  597. for size: A class should have a virtual destructor unless that class has no
  598. virtual functions.  Rationale: if you have any virtual functions at all, you're
  599. probably going to be doing "stuff" to derived objects via a base pointer, and
  600. some of the "stuff" you may do may include invoking a destructor (normally done
  601. implicitly via delete).  Plus once you've put the first virtual function into a
  602. class, you've already paid all the per-object space cost that you'll ever pay
  603. (one pointer per object; note that this is theoretically compiler-specific; in
  604. practice everyone does it pretty much the same way), so making the destructor
  605. virtual won't generally cost you anything extra.
  606.  
  607. ==============================================================================
  608.  
  609. [20.5] What is a "virtual constructor"? [UPDATED!]
  610.  
  611. [Recently added the paragraph on Covariant Return Types (on 3/00).]
  612.  
  613. An idiom that allows you to do something that C++ doesn't directly support.
  614.  
  615. You can get the effect of a virtual constructor by a virtual clone() member
  616. function (for copy constructing), or a virtual create() member function (for
  617. the default constructor[10.4]).
  618.  
  619.     class Shape {
  620.     public:
  621.       virtual ~Shape() { }                 // A virtual destructor[20.4]
  622.       virtual void draw() = 0;             // A pure virtual function[22.4]
  623.       virtual void move() = 0;
  624.       // ...
  625.       virtual Shape* clone()  const = 0;   // Uses the copy constructor
  626.       virtual Shape* create() const = 0;   // Uses the default constructor[10.4]
  627.     };
  628.  
  629.     class Circle : public Shape {
  630.     public:
  631.       Circle* clone()  const { return new Circle(*this); }
  632.       Circle* create() const { return new Circle();      }
  633.       // ...
  634.     };
  635.  
  636. In the clone() member function, the new Circle(*this) code calls Circle's copy
  637. constructor to copy the state of this into the newly created Circle object.  In
  638. the create() member function, the new Circle() code calls Circle's default
  639. constructor[10.4].
  640.  
  641. Users use these as if they were "virtual constructors":
  642.  
  643.     void userCode(Shape& s)
  644.     {
  645.       Shape* s2 = s.clone();
  646.       Shape* s3 = s.create();
  647.       // ...
  648.       delete s2;    // You probably need a virtual destructor[20.4] here
  649.       delete s3;
  650.     }
  651.  
  652. This function will work correctly regardless of whether the Shape is a Circle,
  653. Square, or some other kind-of Shape that doesn't even exist yet.
  654.  
  655. Note: The return type of Circle's clone() member function is intentionally
  656. different from the return type of Shape's clone() member function.  This is
  657. called Covariant Return Types, a feature that was not originally part of the
  658. language.  If your compiler complains at the declaration of
  659. Circle* clone() const within class Circle (e.g., saying "The return type is
  660. different" or "The member function's type differs from the base class virtual
  661. function by return type alone"), you have an old compiler and you'll have to
  662. change the return type to Shape*.
  663.  
  664. ==============================================================================
  665.  
  666. SECTION [21]: Inheritance -- proper inheritance and substitutability
  667.  
  668.  
  669. [21.1] Should I hide member functions that were public in my base class?
  670.  
  671. Never, never, never do this.  Never.  Never!
  672.  
  673. Attempting to hide (eliminate, revoke, privatize) inherited public member
  674. functions is an all-too-common design error.  It usually stems from muddy
  675. thinking.
  676.  
  677. (Note: this FAQ has to do with public inheritance; private and protected
  678. inheritance[24] are different.)
  679.  
  680. ==============================================================================
  681.  
  682. [21.2] Derived* --> Base* works OK; why doesn't Derived** --> Base** work?
  683.  
  684. C++ allows a Derived* to be converted to a Base*, since a Derived object is a
  685. kind of a Base object.  However trying to convert a Derived** to a Base** is
  686. flagged as an error.  Although this error may not be obvious, it is nonetheless
  687. a good thing.  For example, if you could convert a Car** to a Vehicle**, and if
  688. you could similarly convert a NuclearSubmarine** to a Vehicle**, you could
  689. assign those two pointers and end up making a Car* point at a NuclearSubmarine:
  690.  
  691.     class Vehicle {
  692.     public:
  693.       virtual ~Vehicle() { }
  694.       virtual void startEngine() = 0;
  695.     };
  696.  
  697.     class Car : public Vehicle {
  698.     public:
  699.       virtual void startEngine();
  700.       virtual void openGasCap();
  701.     };
  702.  
  703.     class NuclearSubmarine : public Vehicle {
  704.     public:
  705.       virtual void startEngine();
  706.       virtual void fireNuclearMissle();
  707.     };
  708.  
  709.     int main()
  710.     {
  711.       Car   car;
  712.       Car*  carPtr = &car;
  713.       Car** carPtrPtr = &carPtr;
  714.       Vehicle** vehiclePtrPtr = carPtrPtr;  // This is an error in C++
  715.       NuclearSubmarine  sub;
  716.       NuclearSubmarine* subPtr = ⊂
  717.       *vehiclePtrPtr = subPtr;
  718.       // This last line would have caused carPtr to point to sub !
  719.       carPtr->openGasCap();  // This might call fireNuclearMissle()!
  720.     }
  721.  
  722. In other words, if it was legal to convert a Derived** to a Base**, the Base**
  723. could be dereferenced (yielding a Base*), and the Base* could be made to point
  724. to an object of a different derived class, which could cause serious problems
  725. for national security (who knows what would happen if you invoked the
  726. openGasCap() member function on what you thought was a Car, but in reality it
  727. was a NuclearSubmarine!! Try the above code out and see what it does -- on most
  728. compilers it will call NuclearSubmarine::fireNuclearMissle()!
  729.  
  730. (Note: this FAQ has to do with public inheritance; private and protected
  731. inheritance[24] are different.)
  732.  
  733. ==============================================================================
  734.  
  735. [21.3] Is a parking-lot-of-Car a kind-of parking-lot-of-Vehicle?
  736.  
  737. Nope.
  738.  
  739. I know it sounds strange, but it's true.  You can think of this as a direct
  740. consequence of the previous FAQ, or you can reason it this way: if the kind-of
  741. relationship were valid, then someone could point a parking-lot-of-Vehicle
  742. pointer at a parking-lot-of-Car.  But parking-lot-of-Vehicle has a
  743. addNewVehicleToParkingLot(Vehicle&) member function which can add any Vehicle
  744. object to the parking lot.  This would allow you to park a NuclearSubmarine in
  745. a parking-lot-of-Car.  Certainly it would be surprising if someone removed what
  746. they thought was a Car from the parking-lot-of-Car, only to find that it is
  747. actually a NuclearSubmarine.
  748.  
  749. Another way to say this truth: a container of Thing is not a kind-of container
  750. of Anything even if a Thing is a kind-of an Anything.  Swallow hard; it's true.
  751.  
  752. You don't have to like it.  But you do have to accept it.
  753.  
  754. One last example which we use in our OO/C++ training courses: "A Bag-of-Apple
  755. is not a kind-of Bag-of-Fruit." If a Bag-of-Apple could be passed as a
  756. Bag-of-Fruit, someone could put a Banana into the Bag, even though it is
  757. supposed to only contain Apples!
  758.  
  759. (Note: this FAQ has to do with public inheritance; private and protected
  760. inheritance[24] are different.)
  761.  
  762. ==============================================================================
  763.  
  764. [21.4] Is an array of Derived a kind-of array of Base?
  765.  
  766. Nope.
  767.  
  768. This is a corollary of the previous FAQ.  Unfortunately this one can get you
  769. into a lot of hot water.  Consider this:
  770.  
  771.     class Base {
  772.     public:
  773.       virtual void f();             // 1
  774.     };
  775.  
  776.     class Derived : public Base {
  777.     public:
  778.       // ...
  779.     private:
  780.       int i_;                       // 2
  781.     };
  782.  
  783.     void userCode(Base* arrayOfBase)
  784.     {
  785.       arrayOfBase[1].f();           // 3
  786.     }
  787.  
  788.     int main()
  789.     {
  790.       Derived arrayOfDerived[10];   // 4
  791.       userCode(arrayOfDerived);     // 5
  792.     }
  793.  
  794. The compiler thinks this is perfectly type-safe.  Line 5 converts a Derived* to
  795. a Base*.  But in reality it is horrendously evil: since Derived is larger than
  796. Base, the pointer arithmetic done on line 3 is incorrect: the compiler uses
  797. sizeof(Base) when computing the address for arrayOfBase[1], yet the array is an
  798. array of Derived, which means the address computed on line 3 (and the
  799. subsequent invocation of member function f()) isn't even at the beginning of
  800. any object! It's smack in the middle of a Derived object.  Assuming your
  801. compiler uses the usual approach to virtual[20] functions, this will
  802. reinterpret the int i_ of the first Derived as if it pointed to a virtual
  803. table, it will follow that "pointer" (which at this point means we're digging
  804. stuff out of a random memory location), and grab one of the first few words of
  805. memory at that location and interpret them as if they were the address of a C++
  806. member function, then load that (random memory location) into the instruction
  807. pointer and begin grabbing machine instructions from that memory location.  The
  808. chances of this crashing are very high.
  809.  
  810. The root problem is that C++ can't distinguish between a pointer-to-a-thing and
  811. a pointer-to-an-array-of-things.  Naturally C++ "inherited" this feature from
  812. C.
  813.  
  814. NOTE: If we had used an array-like class (e.g., vector<Derived> from STL[32.1])
  815. instead of using a raw array, this problem would have been properly trapped as
  816. an error at compile time rather than a run-time disaster.
  817.  
  818. (Note: this FAQ has to do with public inheritance; private and protected
  819. inheritance[24] are different.)
  820.  
  821. ==============================================================================
  822.  
  823. [21.5] Does array-of-Derived is-not-a-kind-of array-of-Base mean arrays are
  824.        bad?
  825.  
  826. Yes, arrays are evil.  (only half kidding).
  827.  
  828. Seriously, arrays are very closely related to pointers, and pointers are
  829. notoriously difficult to deal with.  But if you have a complete grasp of why
  830. the above few FAQs were a problem from a design perspective (e.g., if you
  831. really know why a container of Thing is not a kind-of container of Anything),
  832. and if you think everyone else who will be maintaining your code also has a
  833. full grasp on these OO design truths, then you should feel free to use arrays.
  834. But if you're like most people, you should use a template container class such
  835. as vector<T> from STL[32.1] rather than raw arrays.
  836.  
  837. (Note: this FAQ has to do with public inheritance; private and protected
  838. inheritance[24] are different.)
  839.  
  840. ==============================================================================
  841.  
  842. [21.6] Is a Circle a kind-of an Ellipse?
  843.  
  844. Not if Ellipse promises to be able to change its size asymmetrically.
  845.  
  846. For example, suppose Ellipse has a setSize(x,y) member function, and suppose
  847. this member function promises the Ellipse's width() will be x, and its height()
  848. will be y.  In this case, Circle can't be a kind-of Ellipse.  Simply put, if
  849. Ellipse can do something Circle can't, then Circle can't be a kind of Ellipse.
  850.  
  851. This leaves two potential (valid) relationships between Circle and Ellipse:
  852.  * Make Circle and Ellipse completely unrelated classes
  853.  * Derive Circle and Ellipse from a base class representing "Ellipses that
  854.    can't necessarily perform an unequal-setSize() operation"
  855.  
  856. In the first case, Ellipse could be derived from class AsymmetricShape, and
  857. setSize(x,y) could be introduced in AsymmetricShape.  However Circle could be
  858. derived from SymmetricShape which has a setSize(size) member function.
  859.  
  860. In the second case, class Oval could only have setSize(size) which sets both
  861. the width() and the height() to size.  Ellipse and Circle could both inherit
  862. from Oval.  Ellipse --but not Circle-- could add the setSize(x,y) operation
  863. (but beware of the hiding rule[23.3] if the same member function name setSize()
  864. is used for both operations).
  865.  
  866. (Note: this FAQ has to do with public inheritance; private and protected
  867. inheritance[24] are different.)
  868.  
  869. (Note: setSize(x,y) isn't sacred.  Depending on your goals, it may be okay to
  870. prevent users from changing the dimensions of an Ellipse, in which case it
  871. would be a valid design choice to not have a setSize(x,y) method in Ellipse.
  872. However this series of FAQs discusses what to do when you want to create a
  873. derived class of a pre-existing base class that has an "unacceptable" method in
  874. it.  Of course the ideal situation is to discover this problem when the base
  875. class doesn't yet exist.  But life isn't always ideal...)
  876.  
  877. ==============================================================================
  878.  
  879. [21.7] Are there other options to the "Circle is/isnot kind-of Ellipse"
  880.        dilemma?
  881.  
  882. If you claim that all Ellipses can be squashed asymmetrically, and you claim
  883. that Circle is a kind-of Ellipse, and you claim that Circle can't be squashed
  884. asymmetrically, clearly you've got to adjust (revoke, actually) one of your
  885. claims.  Thus you've either got to get rid of Ellipse::setSize(x,y), get rid of
  886. the inheritance relationship between Circle and Ellipse, or admit that your
  887. Circles aren't necessarily circular.
  888.  
  889. Here are the two most common traps new OO/C++ programmers regularly fall into.
  890. They attempt to use coding hacks to cover up a broken design (they redefine
  891. Circle::setSize(x,y) to throw an exception, call abort(), choose the average of
  892. the two parameters, or to be a no-op).  Unfortunately all these hacks will
  893. surprise users, since users are expecting width() == x and height() == y.  The
  894. one thing you must not do is surprise your users.
  895.  
  896. If it is important to you to retain the "Circle is a kind-of Ellipse"
  897. inheritance relationship, you can weaken the promise made by Ellipse's
  898. setSize(x,y).  E.g., you could change the promise to, "This member function
  899. might set width() to x and/or it might set height() to y, or it might do
  900. nothing".  Unfortunately this dilutes the contract into dribble, since the user
  901. can't rely on any meaningful behavior.  The whole hierarchy therefore begins to
  902. be worthless (it's hard to convince someone to use an object if you have to
  903. shrug your shoulders when asked what the object does for them).
  904.  
  905. (Note: this FAQ has to do with public inheritance; private and protected
  906. inheritance[24] are different.)
  907.  
  908. (Note: setSize(x,y) isn't sacred.  Depending on your goals, it may be okay to
  909. prevent users from changing the dimensions of an Ellipse, in which case it
  910. would be a valid design choice to not have a setSize(x,y) method in Ellipse.
  911. However this series of FAQs discusses what to do when you want to create a
  912. derived class of a pre-existing base class that has an "unacceptable" method in
  913. it.  Of course the ideal situation is to discover this problem when the base
  914. class doesn't yet exist.  But life isn't always ideal...)
  915.  
  916. ==============================================================================
  917.  
  918. [21.8] But I have a Ph.D. in Mathematics, and I'm sure a Circle is a kind of an
  919.        Ellipse! Does this mean Marshall Cline is stupid? Or that C++ is stupid?
  920.        Or that OO is stupid?
  921.  
  922. Actually, it doesn't mean any of these things.  The sad reality is that it
  923. means your intuition is wrong.
  924.  
  925. Look, I have received and answered dozens of passionate e-mail messages about
  926. this subject.  I have taught it hundreds of times to thousands of software
  927. professionals all over the place.  I know it goes against your intuition.  But
  928. trust me; your intuition is wrong.
  929.  
  930. The real problem is your intuitive notion of "kind of" doesn't match the OO
  931. notion of proper inheritance (technically called "subtyping").  The bottom line
  932. is that the derived class objects must be substitutable for the base class
  933. objects.  In the case of Circle/Ellipse, the setSize(x,y) member function
  934. violates this substitutability.
  935.  
  936. You have three choices: [1] remove the setSize(x,y) member function from
  937. Ellipse (thus breaking existing code that calls the setSize(x,y) member
  938. function), [2] allow a Circle to have a different height than width (an
  939. asymmetrical circle; hmmm), or [3] drop the inheritance relationship.  Sorry,
  940. but there simply are no other choices.  Note that some people mention the
  941. option of deriving both Circle and Ellipse from a third common base class, but
  942. that's just a variant of option [3] above.
  943.  
  944. Another way to say this is that you have to either make the base class weaker
  945. (in this case braindamage Ellipse to the point that you can't set its width and
  946. height to different values), or make the derived class stronger (in this case
  947. empower a Circle with the ability to be both symmetric and, ahem, asymmetric).
  948. When neither of these is very satisfying (such as in the Circle/Ellipse case),
  949. one normally simply removes the inheritance relationship.  If the inheritance
  950. relationship simply has to exist, you may need to remove the mutator member
  951. functions (setHeight(y), setWidth(x), and setSize(x,y)) from the base class.
  952.  
  953. (Note: this FAQ has to do with public inheritance; private and protected
  954. inheritance[24] are different.)
  955.  
  956. (Note: setSize(x,y) isn't sacred.  Depending on your goals, it may be okay to
  957. prevent users from changing the dimensions of an Ellipse, in which case it
  958. would be a valid design choice to not have a setSize(x,y) method in Ellipse.
  959. However this series of FAQs discusses what to do when you want to create a
  960. derived class of a pre-existing base class that has an "unacceptable" method in
  961. it.  Of course the ideal situation is to discover this problem when the base
  962. class doesn't yet exist.  But life isn't always ideal...)
  963.  
  964. ==============================================================================
  965.  
  966. [21.9] But my problem doesn't have anything to do with circles and ellipses, so
  967.        what good is that silly example to me?
  968.  
  969. Ahhh, there's the rub.  You think the Circle/Ellipse example is just a silly
  970. example.  But in reality, your problem is an isomorphism to that example.
  971.  
  972. I don't care what your inheritance problem is, but all (yes all) bad
  973. inheritances boil down to the Circle-is-not-a-kind-of-Ellipse example.
  974.  
  975. Here's why: Bad inheritances always have a base class with an extra capability
  976. (often an extra member function or two; sometimes an extra promise made by one
  977. or a combination of member functions) that a derived class can't satisfy.
  978. You've either got to make the base class weaker, make the derived class
  979. stronger, or eliminate the proposed inheritance relationship.  I've seen lots
  980. and lots and lots of these bad inheritance proposals, and believe me, they all
  981. boil down to the Circle/Ellipse example.
  982.  
  983. Therefore, if you truly understand the Circle/Ellipse example, you'll be able
  984. to recognize bad inheritance everywhere.  If you don't understand what's going
  985. on with the Circle/Ellipse problem, the chances are high that you'll make some
  986. very serious and very expensive inheritance mistakes.
  987.  
  988. Sad but true.
  989.  
  990. (Note: this FAQ has to do with public inheritance; private and protected
  991. inheritance[24] are different.)
  992.  
  993. ==============================================================================
  994.  
  995. SECTION [22]: Inheritance -- abstract base classes (ABCs)
  996.  
  997.  
  998. [22.1] What's the big deal of separating interface from implementation?
  999.  
  1000. Interfaces are a company's most valuable resources.  Designing an interface
  1001. takes longer than whipping together a concrete class which fulfills that
  1002. interface.  Furthermore interfaces require the time of more expensive people.
  1003.  
  1004. Since interfaces are so valuable, they should be protected from being tarnished
  1005. by data structures and other implementation artifacts.  Thus you should
  1006. separate interface from implementation.
  1007.  
  1008. ==============================================================================
  1009.  
  1010. [22.2] How do I separate interface from implementation in C++ (like Modula-2)?
  1011.  
  1012. Use an ABC[22.3].
  1013.  
  1014. ==============================================================================
  1015.  
  1016. [22.3] What is an ABC?
  1017.  
  1018. An abstract base class.
  1019.  
  1020. At the design level, an abstract base class (ABC) corresponds to an abstract
  1021. concept.  If you asked a mechanic if he repaired vehicles, he'd probably wonder
  1022. what kind-of vehicle you had in mind.  Chances are he doesn't repair space
  1023. shuttles, ocean liners, bicycles, or nuclear submarines.  The problem is that
  1024. the term "vehicle" is an abstract concept (e.g., you can't build a "vehicle"
  1025. unless you know what kind of vehicle to build).  In C++, class Vehicle would be
  1026. an ABC, with Bicycle, SpaceShuttle, etc, being subclasses (an OceanLiner
  1027. is-a-kind-of-a Vehicle).  In real-world OO, ABCs show up all over the place.
  1028.  
  1029. At the programming language level, an ABC is a class that has one or more pure
  1030. virtual[22.4] member functions.  You cannot make an object (instance) of an
  1031. ABC.
  1032.  
  1033. ==============================================================================
  1034.  
  1035. [22.4] What is a "pure virtual" member function?
  1036.  
  1037. A member function declaration that turns a normal class into an abstract class
  1038. (i.e., an ABC).  You normally only implement it in a derived class.
  1039.  
  1040. Some member functions exist in concept; they don't have any reasonable
  1041. definition.  E.g., suppose I asked you to draw a Shape at location (x,y) that
  1042. has size 7.  You'd ask me "what kind of shape should I draw?" (circles,
  1043. squares, hexagons, etc, are drawn differently).  In C++, we must indicate the
  1044. existence of the draw() member function (so users can call it when they have a
  1045. Shape* or a Shape&), but we recognize it can (logically) be defined only in
  1046. subclasses:
  1047.  
  1048.     class Shape {
  1049.     public:
  1050.       virtual void draw() const = 0;  // = 0 means it is "pure virtual"
  1051.       // ...
  1052.     };
  1053.  
  1054. This pure virtual function makes Shape an ABC.  If you want, you can think of
  1055. the "= 0;" syntax as if the code were at the NULL pointer.  Thus Shape promises
  1056. a service to its users, yet Shape isn't able to provide any code to fulfill
  1057. that promise.  This forces any actual object created from a [concrete] class
  1058. derived from Shape to have the indicated member function, even though the base
  1059. class doesn't have enough information to actually define it yet.
  1060.  
  1061. Note that it is possible to provide a definition for a pure virtual function,
  1062. but this usually confuses novices and is best avoided until later.
  1063.  
  1064. ==============================================================================
  1065.  
  1066. [22.5] How do you define a copy constructor or assignment operator for a class
  1067.        that contains a pointer to a (abstract) base class? [UPDATED!]
  1068.  
  1069. [Recently fixed Circle and Square so they inherit from Shape thanks to Paul
  1070. Campbell (on 1/00).]
  1071.  
  1072. If the class "owns" the object pointed to by the (abstract) base class pointer,
  1073. use the Virtual Constructor Idiom[20.5] in the (abstract) base class.  As usual
  1074. with this idiom, we declare a pure virtual[22.4] clone() method in the base
  1075. class:
  1076.  
  1077.     class Shape {
  1078.     public:
  1079.       // ...
  1080.       virtual Shape* clone() const = 0;   // The Virtual (Copy) Constructor[20.5]
  1081.       // ...
  1082.     };
  1083.  
  1084. Then we implement this clone() method in each derived class:
  1085.  
  1086.     class Circle : public Shape {
  1087.     public:
  1088.       // ...
  1089.       virtual Shape* clone() const { return new Circle(*this); }
  1090.       // ...
  1091.     };
  1092.  
  1093.     class Square : public Shape {
  1094.     public:
  1095.       // ...
  1096.       virtual Shape* clone() const { return new Square(*this); }
  1097.       // ...
  1098.     };
  1099.  
  1100. Now suppose that each Fred object "has-a" Shape object.  Naturally the Fred
  1101. object doesn't know whether the Shape is Circle or a Square or ...  Fred's copy
  1102. constructor and assignment operator will invoke Shape's clone() method to copy
  1103. the object:
  1104.  
  1105.     class Fred {
  1106.     public:
  1107.       Fred(Shape* p) : p_(p) { assert(p != NULL); }   // p must not be NULL
  1108.      ~Fred() { delete p_; }
  1109.       Fred(const Fred& f) : p_(f.p_->clone()) { }
  1110.       Fred& operator= (const Fred& f)
  1111.         {
  1112.           if (this != &f) {              // Check for self-assignment
  1113.             Shape* p2 = f.p_->clone();   // Create the new one FIRST...
  1114.             delete p_;                   // ...THEN delete the old one
  1115.             p_ = p2;
  1116.           }
  1117.           return *this;
  1118.         }
  1119.       // ...
  1120.     private:
  1121.       Shape* p_;
  1122.     };
  1123.  
  1124. ==============================================================================
  1125.  
  1126. SECTION [23]: Inheritance -- what your mother never told you
  1127.  
  1128.  
  1129. [23.1] When my base class's constructor calls a virtual function, why doesn't
  1130.        my derived class's override of that virtual function get invoked?
  1131.  
  1132. During the class Base's constructor, the object isn't yet a Derived, so if
  1133. Base::Base() calls a virtual[20] function virt(), the Base::virt() will be
  1134. invoked, even if Derived::virt() exists.
  1135.  
  1136. Similarly, during Base's destructor, the object is no longer a Derived, so when
  1137. Base::~Base() calls virt(), Base::virt() gets control, not the Derived::virt()
  1138. override.
  1139.  
  1140. You'll quickly see the wisdom of this approach when you imagine the disaster if
  1141. Derived::virt() touched a member object from class Derived.  In particular, if
  1142. Base::Base() called the virtual function virt(), this rule causes Base::virt()
  1143. to be invoked.  If it weren't for this rule, Derived::virt() would get called
  1144. before the Derived part of a Derived object is constructed, and Derived::virt()
  1145. could touch unconstructed member objects from the Derived part of a Derived
  1146. object.  That would be a disaster.
  1147.  
  1148. ==============================================================================
  1149.  
  1150. [23.2] Should a derived class replace ("override") a non-virtual function from
  1151.        a base class?
  1152.  
  1153. It's legal, but it ain't moral.
  1154.  
  1155. Experienced C++ programmers will sometimes redefine a non-virtual function for
  1156. efficiency (e.g., if the derived class implementation can make better use of
  1157. the derived class's resources) or to get around the hiding rule[23.3].  However
  1158. the client-visible effects must be identical, since non-virtual functions are
  1159. dispatched based on the static type of the pointer/reference rather than the
  1160. dynamic type of the pointed-to/referenced object.
  1161.  
  1162. ==============================================================================
  1163.  
  1164. [23.3] What's the meaning of, Warning: Derived::f(float) hides Base::f(int)?
  1165.  
  1166. It means you're going to die.
  1167.  
  1168. Here's the mess you're in: if Base declares a member function f(int), and
  1169. Derived declares a member function f(float) (same name but different parameter
  1170. types and/or constness), then the Base f(int) is "hidden" rather than
  1171. "overloaded" or "overridden" (even if the Base f(int) is virtual[20]).
  1172.  
  1173. Here's how you get out of the mess: Derived must have a using declaration of
  1174. the hidden member function.  For example,
  1175.  
  1176.     class Base {
  1177.     public:
  1178.       void f(int);
  1179.     };
  1180.  
  1181.     class Derived : public Base {
  1182.     public:
  1183.       using Base::f;    // This un-hides Base::f(int)
  1184.       void f(double);
  1185.     };
  1186.  
  1187. If the using syntax isn't supported by your compiler, redefine the hidden Base
  1188. member function(s), even if they are non-virtual[23.2].  Normally this
  1189. re-definition merely calls the hidden Base member function using the :: syntax.
  1190. E.g.,
  1191.  
  1192.     class Derived : public Base {
  1193.     public:
  1194.       void f(double);
  1195.       void f(int i) { Base::f(i); }  // The redefinition merely calls Base::f(int)
  1196.     };
  1197.  
  1198. ==============================================================================
  1199.  
  1200. [23.4] What does it mean that the "virtual table" is an unresolved external?
  1201.  
  1202. If you get a link error of the form
  1203. "Error: Unresolved or undefined symbols detected: virtual table for class Fred,"
  1204. you probably have an undefined virtual[20] member function in class Fred.
  1205.  
  1206. The compiler typically creates a magical data structure called the "virtual
  1207. table" for classes that have virtual functions (this is how it handles dynamic
  1208. binding[20.2]).  Normally you don't have to know about it at all.  But if you
  1209. forget to define a virtual function for class Fred, you will sometimes get this
  1210. linker error.
  1211.  
  1212. Here's the nitty gritty: Many compilers put this magical "virtual table" in the
  1213. compilation unit that defines the first non-inline virtual function in the
  1214. class.  Thus if the first non-inline virtual function in Fred is wilma(), the
  1215. compiler will put Fred's virtual table in the same compilation unit where it
  1216. sees Fred::wilma().  Unfortunately if you accidentally forget to define
  1217. Fred::wilma(), rather than getting a Fred::wilma() is undefined, you may get a
  1218. "Fred's virtual table is undefined".  Sad but true.
  1219.  
  1220. ==============================================================================
  1221.  
  1222. -- 
  1223. Marshall Cline / 972-931-9470 / mailto:cline@parashift.com
  1224.