home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!spool.mu.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!uunet.ca!ohrd!twriter
- From: twriter@rd.hydro.on.ca (Timothy Writer)
- Subject: Re: Overriding the order of constructors?
- Message-ID: <1992Dec14.204903.10047@rd.hydro.on.ca>
- Keywords: constructor ordering
- Reply-To: twriter@rd.hydro.on.ca
- Organization: "Ontario Hydro - Research Division"
- References: <1992Dec13.220943.22047@jarvis.csri.toronto.edu> <robison1.724307343@husc10> <Bz8oGx.B7x@well.sf.ca.us>
- Date: Mon, 14 Dec 92 20:49:03 GMT
- Lines: 62
-
- johnrp@well.sf.ca.us (John Panzer) writes:
-
- >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? 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.
- >>
-
- >Are you certain? I believe that A::reroute() will
- >be called if reroute() is called from within A's
- >constructor or destructor. I think this is a
- >common C++ "gotcha" - it's certainly gotten me
- >in the past.
-
- You (robison1@husc10.harvard.edu) are right. To quote from the ARM p.294:
-
- Member functions may be called in constructors and destructors.
- This implies that virtual functions may be called (directly or
- indirectly). The function called will be the one defined in the
- constructor's (or destructor's) OWN class or its bases, but NOT
- any function overriding it in a derived class. This ensures
- that unconstructed objects will not be accessed during
- construction or destruction.
-
- In other words, although your (dembo@csri.toronto.edu) example is
- syntactically correct, it does not have the desired result.
-
- Perhaps the original poster could porovide a more concrete example of
- what he hopes to achieve by specifying the order of constructor
- execution. Someone may be able to suggest an alternative solution.
-
- Tim
-
- --
- Tim Writer phone: (416) 231-4111 ext. 6990
- Ontario Hydro Research Division fax: (416) 237-9285
- Toronto, Ontario e-mail: twriter@rd.hydro.on.ca
- CANADA
-