home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19873 < prev    next >
Encoding:
Text File  |  1993-01-25  |  2.3 KB  |  54 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: When Are Global Objects with a Constructor Initialized?
  5. Message-ID: <1993Jan25.174602.20429@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1993Jan25.024402.10801@alchemy.chem.utoronto.ca>
  8. Date: Mon, 25 Jan 1993 17:46:02 GMT
  9. Lines: 43
  10.  
  11. mbersohn@alchemy.chem.utoronto.ca (M. Bersohn) writes:
  12.  
  13. >    Does anyone know when global objects of a class with
  14. >a constructor that does initializations are initialized,
  15. >at compile time or at the beginning of execution?
  16.  
  17. Global objects with constructors are initialized before the first
  18. statement of main() is executed.  Within one compilation unit, they
  19. are initialized in definition order, except that objects with a
  20. default initialization to zero are initialized first.  The C++
  21. Committee is working on a way to specify that objects initialized with
  22. constant expressions get initialized before other objects.  (Current
  23. implementations usually work this way anyhow.)
  24.  
  25. >P.S. I'm aware that aggregates with an initialization list
  26. >are initialized at compile time, but a constructor looks
  27. >like code that is executed.
  28.  
  29. "Compile-time initialization" is not a C++ concept (nor a C concept).
  30. For example, consider this entire translation unit:
  31.     extern int i;
  32.     int *ip = &i;
  33. This is legal code in C and C++, yet the compiler (assuming a separate
  34. linker) cannot provide the value for the initialization of "ip".
  35. Suppose further that "i" is in a shared, dynamically-linked library.
  36. We would not even have link-time initialization in that case.  The
  37. address is not known until the program is loaded into memory and the
  38. program loader (not the object-code linker) finds the library.
  39.  
  40. C requires that objects with static duration be initialized only
  41. with constant expressions (without going into all the details).
  42.  
  43. C++ allows such objects to be initialized with anything which may
  44. be used to initialize a dynamic object of the same type.  As noted
  45. above, current implementations usually arrange things so that
  46. initialization with constant expressions occurs prior to the program
  47. beginning execution, thus requiring no run-time code.  Objects with
  48. non-constant initializers generally require run-time code which is
  49. part of the program.
  50.  
  51. -- 
  52.  
  53. Steve Clamage, TauMetric Corp, steve@taumet.com
  54.