home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: When Are Global Objects with a Constructor Initialized?
- Message-ID: <1993Jan25.174602.20429@taumet.com>
- Organization: TauMetric Corporation
- References: <1993Jan25.024402.10801@alchemy.chem.utoronto.ca>
- Date: Mon, 25 Jan 1993 17:46:02 GMT
- Lines: 43
-
- mbersohn@alchemy.chem.utoronto.ca (M. Bersohn) writes:
-
- > Does anyone know when global objects of a class with
- >a constructor that does initializations are initialized,
- >at compile time or at the beginning of execution?
-
- Global objects with constructors are initialized before the first
- statement of main() is executed. Within one compilation unit, they
- are initialized in definition order, except that objects with a
- default initialization to zero are initialized first. The C++
- Committee is working on a way to specify that objects initialized with
- constant expressions get initialized before other objects. (Current
- implementations usually work this way anyhow.)
-
- >P.S. I'm aware that aggregates with an initialization list
- >are initialized at compile time, but a constructor looks
- >like code that is executed.
-
- "Compile-time initialization" is not a C++ concept (nor a C concept).
- For example, consider this entire translation unit:
- extern int i;
- int *ip = &i;
- This is legal code in C and C++, yet the compiler (assuming a separate
- linker) cannot provide the value for the initialization of "ip".
- Suppose further that "i" is in a shared, dynamically-linked library.
- We would not even have link-time initialization in that case. The
- address is not known until the program is loaded into memory and the
- program loader (not the object-code linker) finds the library.
-
- C requires that objects with static duration be initialized only
- with constant expressions (without going into all the details).
-
- C++ allows such objects to be initialized with anything which may
- be used to initialize a dynamic object of the same type. As noted
- above, current implementations usually arrange things so that
- initialization with constant expressions occurs prior to the program
- beginning execution, thus requiring no run-time code. Objects with
- non-constant initializers generally require run-time code which is
- part of the program.
-
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
-