[net.lang.c++] Implementing statics with constructors

hymie@dvm.UUCP (Hyman Rosen) (11/04/86)

It is clear from the C++ book that static variables of types with
constructors are initialized by calls to the constructors at some
point before the call to main(). I would like to know how the
various implementations of C++ arrange for this to happen. It seems
to be difficult to do without a system of multiple code segments
(where all initialization code could be brought together). Other-
wise, I can only think of expensive "is this the first time?"
checking.
-- 


								- Hymie

		...{decvax,ihnp4,ucbvax}!allegra!phri!orville!dvm!hymie

crowl@rochester.ARPA (Lawrence Crowl) (11/04/86)

In article <5@dvm.UUCP> hymie@dvm.UUCP (Hyman Rosen) writes:
)It is clear from the C++ book that static variables of types with
)constructors are initialized by calls to the constructors at some
)point before the call to main(). I would like to know how the
)various implementations of C++ arrange for this to happen. It seems
)to be difficult to do without a system of multiple code segments
)(where all initialization code could be brought together). Other-
)wise, I can only think of expensive "is this the first time?"
)checking.

The compiler generates a routine to call all of the static constructors
within each individual source file.  These routines have special names
and are placed in the .o file.  When the .o files are linked together,
the names of the constructors (and destructors) are extracted and
called from a routine created (sort of) at link time.  It is this created
routine that is called before main().  Because this model doesn't fit with
the standard compiler and linker model, the details are kind of ugly.
-- 
  Lawrence Crowl		716-275-5766	University of Rochester
			crowl@rochester.arpa	Computer Science Department
 ...!{allegra,decvax,seismo}!rochester!crowl	Rochester, New York,  14627