home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!enterpoop.mit.edu!eru.mt.luth.se!hagbard!loglule!jbn
- From: jbn@lulea.trab.se (Johan Bengtsson)
- Newsgroups: comp.lang.c++
- Subject: Re: Overriding the order of constructors?
- Message-ID: <5375@holden.lulea.trab.se>
- Date: 14 Dec 92 18:03:28 GMT
- References: <robison1.724307343@husc10>
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 66
- X-Newsreader: TIN [version 1.1 + PL8]
-
- Keith Robison (robison1@husc10.harvard.edu) wrote:
- : dembo@csri.toronto.edu (Prof. Ron Dembo) writes:
-
- : >What is the rational for not allowing a programmer to override the
- : >default calling order of constructors and initializers? I see
- : >the importance of having a well defined default. However, why
- : >shouldn't the ordering of the initilizers be significant?
-
-
- : I believe you can effectively accomplish this dangerous
- : goal by calling virtual functions in your constructors:
-
- : class A {
- : A() { reroute(); do_A_ctor_stuff(); }
- : virtual void reroute(); { }
- : }
-
- : class B : public A {
- : B() {}
- : void reroute () { do_B_ctor_stuff(); }
- : }
-
-
- : This will cause B's reroute() to be executed before any
- : A constructing has occurred.
-
- Wrong.
-
- When inside A::A() even a B object behaves _exactly_ as though
- it was an A object. In particular, calling virtual functions
- from the constructor of a will _never_ call redefined functions
- in derived classes. I won't comment on the other small errors
- in the suggested code.
-
- [ mini-flame on ]
- You should have tested this idea before posting.
- [ off ]
-
- The following accomplishes what you are looking for:
-
- class Base { public:
- Base() { cout << "ctor Base stuff\n"; }
- protected:
- Base( void (*callback)(Base*), Base* derived )
- {
- if ( callback != 0 ) (*callback)(derived);
- cout << "ctor Base stuff\n";
- }
- };
-
-
- class Derived : public Base { public:
- Derived() : Base(init_derived,this) { }
- friend void init_derived(Base* base);
- };
-
- void init_derived(Base* base)
- {
- Derived* thatShouldBeADerived = (Derived*)base;
- cout << "ctor Derived stuff\n";
- }
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-