home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 17939 < prev    next >
Encoding:
Text File  |  1992-12-14  |  2.3 KB  |  78 lines

  1. Path: sparky!uunet!enterpoop.mit.edu!eru.mt.luth.se!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Overriding the order of constructors?
  5. Message-ID: <5375@holden.lulea.trab.se>
  6. Date: 14 Dec 92 18:03:28 GMT
  7. References: <robison1.724307343@husc10>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 66
  10. X-Newsreader: TIN [version 1.1 + PL8]
  11.  
  12. Keith Robison (robison1@husc10.harvard.edu) wrote:
  13. : dembo@csri.toronto.edu (Prof. Ron Dembo) writes:
  14.  
  15. : >What is the rational for not allowing a programmer to override the
  16. : >default calling order of constructors and initializers?  I see
  17. : >the importance of having a well defined default.  However, why
  18. : >shouldn't the ordering of the initilizers be significant?  
  19.  
  20.  
  21. :     I believe you can effectively accomplish this dangerous
  22. : goal by calling virtual functions in your constructors:
  23.  
  24. : class A {
  25. :   A() { reroute(); do_A_ctor_stuff(); }
  26. :   virtual void reroute(); { }
  27. : }
  28.  
  29. : class B : public A {
  30. :   B() {}
  31. :   void reroute ()  { do_B_ctor_stuff(); }
  32. : }
  33.  
  34.  
  35. : This will cause B's reroute() to be executed before any 
  36. : A constructing has occurred.
  37.  
  38. Wrong.
  39.  
  40. When inside A::A() even a B object behaves _exactly_ as though
  41. it was an A object.  In particular, calling virtual functions
  42. from the constructor of a will _never_ call redefined functions
  43. in derived classes.  I won't comment on the other small errors
  44. in the suggested code.
  45.  
  46. [ mini-flame on ]
  47. You should have tested this idea before posting.
  48. [ off ]
  49.  
  50. The following accomplishes what you are looking for:
  51.  
  52. class Base { public:
  53.   Base() { cout << "ctor Base stuff\n"; }
  54. protected:
  55.   Base( void (*callback)(Base*), Base* derived )
  56.     {
  57.       if ( callback != 0 ) (*callback)(derived);
  58.       cout << "ctor Base stuff\n";
  59.     }
  60. };
  61.  
  62.  
  63. class Derived : public Base { public:
  64.   Derived() : Base(init_derived,this) { }
  65.   friend void init_derived(Base* base);
  66. };
  67.  
  68. void init_derived(Base* base)
  69. {
  70.   Derived* thatShouldBeADerived = (Derived*)base;
  71.   cout << "ctor Derived stuff\n";
  72. }
  73. -- 
  74. --------------------------------------------------------------------------
  75. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  76. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  77. --------------------------------------------------------------------------
  78.