home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!ut-emx!jamshid
- From: jamshid@ut-emx.uucp (Jamshid Afshar)
- Newsgroups: comp.lang.c++
- Subject: Re: Overriding the order of constructors?
- Summary: too complicated; RTFAQ
- Keywords: constructor ordering
- Message-ID: <85513@ut-emx.uucp>
- Date: 17 Dec 92 18:32:34 GMT
- References: <1992Dec13.220943.22047@jarvis.csri.toronto.edu> <robison1.724307343@husc10>
- Reply-To: jamshid@emx.utexas.edu
- Organization: The University of Texas at Austin; Austin, Texas
- Lines: 38
-
- In article <robison1.724307343@husc10> robison1@husc10.harvard.edu (Keith Robison) writes:
- |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?
-
- If you're asking why you can't construct a derived class's data member
- before constructing a base class, I think because that would just add
- complexity and confusion. At least one problem is how to handle the
- order of destructor calls. The class and data member destructors
- should be called in the reverse order their constructors were called.
- If different member initialization lists could specify different
- construction orders, there would be no practical way to know which
- order to destroy the members.
-
- | 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.
-
- No, it will not. Any virtual functions called during a constructor
- call that class's definition of the virtual functon, not a derived
- class's. See FAQ #65 "Why does base ctor get *base*'s virtual fn
- instead of the derived version?". The FAQL is available by anonymous
- ftp at sun.soe.clarkson.edu in pub/C++/FAQ.
-
- Jamshid Afshar
- jamshid@emx.utexas.edu
-