ttwang@polyslo.CalPoly.EDU (Thomas Wang) (10/19/89)
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?
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()
{}
I thought of initializing 'header' in the overloaded 'new' operator, but
that will not work either. In the case of a stack object, the 'new'
operator will not be called.
If there is no official order of calling the constructors, is there any
'defacto' order of calling the constructors?
Intuitively speaking, I think the order of construction should be
'header', 'the_foo', followed by the unmentioned data member 'the_bar'.
In this case, 'header' contains garbage collection information, so it need
to be initialized before the constructors of other data members allocate
any additional storage space.
-Thomas Wang ("This is a fantastic comedy that Ataru and his wife Lum, an
invader from space, cause excitement involving their neighbors."
- from a badly translated Urusei Yatsura poster)
ttwang@polyslo.calpoly.edu
dog@cbnewsl.ATT.COM (edward.n.schiebel) (10/19/89)
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
randolph@ektools.UUCP (Gary L. Randolph) (10/19/89)
In article <1989Oct18.202605.5971@polyslo.CalPoly.EDU> ttwang@polyslo.CalPoly.EDU (Thomas Wang) writes:
#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?
The order has always been defined, but it is defined differently than it
was originally. You can count on data members being constructed:
IN THE ORDER OF DECLARATION WITHIN THE CONTAINING CLASS.
(Never trust me:-)) This is written on page 242 in Lippman.
BTW, in the old days (??) the construction order followed the order of the
member initialization list of the containing class. That's the list after
the colon in the containing class constructor.
#
#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 used to follow ^^^^^^^^^^^^^^^^^^^^^^^^.
BUT NOW THE ORDER FOLLOWS THE ORDER OF DECLARATION.
#{}
Gary Randolph
Eastman Kodak