home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18216 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  1.9 KB

  1. Path: sparky!uunet!cs.utexas.edu!ut-emx!jamshid
  2. From: jamshid@ut-emx.uucp (Jamshid Afshar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Overriding the order of constructors?
  5. Summary: too complicated; RTFAQ
  6. Keywords: constructor ordering
  7. Message-ID: <85513@ut-emx.uucp>
  8. Date: 17 Dec 92 18:32:34 GMT
  9. References: <1992Dec13.220943.22047@jarvis.csri.toronto.edu> <robison1.724307343@husc10>
  10. Reply-To: jamshid@emx.utexas.edu
  11. Organization: The University of Texas at Austin; Austin, Texas
  12. Lines: 38
  13.  
  14. In article <robison1.724307343@husc10> robison1@husc10.harvard.edu (Keith Robison) writes:
  15. |dembo@csri.toronto.edu (Prof. Ron Dembo) writes:
  16. |>What is the rational for not allowing a programmer to override the
  17. |>default calling order of constructors and initializers?
  18.  
  19. If you're asking why you can't construct a derived class's data member
  20. before constructing a base class, I think because that would just add
  21. complexity and confusion.  At least one problem is how to handle the
  22. order of destructor calls.  The class and data member destructors
  23. should be called in the reverse order their constructors were called.
  24. If different member initialization lists could specify different
  25. construction orders, there would be no practical way to know which
  26. order to destroy the members.
  27.  
  28. |    I believe you can effectively accomplish this dangerous
  29. |goal by calling virtual functions in your constructors:
  30. |
  31. |class A {
  32. |  A() { reroute(); do_A_ctor_stuff(); }
  33. |  virtual void reroute(); { }
  34. |}
  35. |
  36. |class B : public A {
  37. |  B() {}
  38. |  void reroute ()  { do_B_ctor_stuff(); }
  39. |}
  40. |
  41. |This will cause B's reroute() to be executed before any 
  42. |A constructing has occurred.
  43.  
  44. No, it will not.  Any virtual functions called during a constructor
  45. call that class's definition of the virtual functon, not a derived
  46. class's.  See FAQ #65 "Why does base ctor get *base*'s virtual fn
  47. instead of the derived version?".  The FAQL is available by anonymous
  48. ftp at sun.soe.clarkson.edu in pub/C++/FAQ.
  49.  
  50. Jamshid Afshar
  51. jamshid@emx.utexas.edu
  52.