Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!tut.cis.ohio-state.edu!att!cbnewsl!dog From: dog@cbnewsl.ATT.COM (edward.n.schiebel) Newsgroups: comp.lang.c++ Subject: Re: order which the constructors are called Message-ID: <2349@cbnewsl.ATT.COM> Date: 19 Oct 89 11:46:42 GMT References: <1989Oct18.202605.5971@polyslo.CalPoly.EDU> Distribution: usa Organization: AT&T Bell Laboratories Lines: 42 From article <1989Oct18.202605.5971@polyslo.CalPoly.EDU>, by ttwang@polyslo.CalPoly.EDU (Thomas Wang): > I have a class with a data member that need to be initialized before all > other data members in that class. > > As I seem to recall that the order of constructing data members is undefined, > I wonder if it's still the case? From the 2.0 Reference Manual, Section 12.6.2 "...First the base classes are initialized in declaration order ( independent of the member-initializers), then the members are initialized in declaration order (independent of the member-initializers), then the body of derived::derived() is executed. The declaration order is used to ensure that sub-objects and members are destroyed in the reverse order of initalization. Virtual base classes constitute a special case..." but let's not get into that :-) Thus in your example > class student > { > public: > student(); // 'header' need to be initialized before 'the_foo' > header_student header; > bar the_bar; > foo the_foo; > }; > > student::student() : header(this), the_foo() > {} The order of initialization is header, the_bar, the_foo. Note this is fixed, and is no different even if the constructor were written: student::student() : the_foo(), header(this) {} Ed Schiebel AT&T Bell Laboratories dog@vilya.att.com 201-386-3416