PRODUCT : Borland C++ NUMBER : 1022
VERSION : 3.1
OS : DOS
DATE : December 3, 1992 PAGE : 1/7
TITLE : Initialization Order of Objects & Turbo Vision
INITIALIZATION ORDER OF GLOBAL OBJECTS BETWEEN SOURCE MODULES
The order of constructor calls for global objects is an
important consideration when writing large-scale C++ programs.
The Annotated C++ Reference Manual (ARM) by Ellis and Stroustrup
is the ANSI base document for C++. It only specifies the order
for a given source module. Any other initialization order is not
specified by the ARM and is therefore implementation dependent.
( NOTE: section 3.4 of the ARM also mentions other issues related
to object initialization before the use of any function/object
defined in a given module - this document, however, discusses the
current implementation of Borland C++ 3.1 and it predecessors.)
According to Ellis and Stroustrup, the order of
initialization in a given module will always be in order of
declaration, in other words, top down and left to right.
Execution order of constructors and destructors between source
modules is not specified, except that destructors will be called
in reverse order from the constructors. (See ARM section 3.4.)
The only exception to this rule is that it is guaranteed that the
I/O streams cout and cin will be constructed before any other
global constructor calls are made.
The effect of this provision is that if you have two modules, A
and B, and each has a global object that has a constructor, say
GlobObjA and GlobObjB respectively, then GlobObjA might be
created before GlobObjB, or it might not. This order is
implementation dependent and should not be relied upon since it
is inherently non-portable and could even change between compiler
versions from the same vendor.
The following text describes how Borland C++ and Turbo C++ handle
the order of construction of global objects.
When building a C++ program, three things can affect the link
order of modules:
1. order of object modules
2. order of libraries
3. order of object modules within a library
For compiler versions 1.0, 1.01, 2.0, and 3.0 (Borland C++ or
Turbo C++, Windows or DOS), the order of constructor calls is
PRODUCT : Borland C++ NUMBER : 1022
VERSION : 3.1
OS : DOS
DATE : December 3, 1992 PAGE : 2/7
TITLE : Initialization Order of Objects & Turbo Vision
from right to left on the TLINK line, starting with libraries,
then object modules, and from bottom to top inside an individual
library. Simply put, the order is exactly backwards to the order
in which the linker found each module.
There is also an anomaly that causes the destructors to be called
in exactly the same order, in violation of the standard.
For compiler version 3.1, the order of constructor calls is
exactly opposite of that listed above. Object module
constructors are called first, from left to right on the TLINK
line. Then library module constructors from left to right on the
TLINK line and top to bottom within each library. Destructor
order has not changed, and thus the 3.1 compilers are now in
accordance with the standard, with destructor order being the
exact opposite of constructor order.
If the IDE is being used, then the order of the object files on
the TLINK command line corresponds to the order in which modules
are listed in the project file. User libraries are treated
similarly.
This fix implemented in version 3.1 affects the Turbo Vision
library. Declaring the application object of a Turbo Vision
program outside of main() (i.e., make it a global object) was
allowed in version 3.0. This was because its constructor would be
called after every other object in the Turbo Vision library had
been constructed. In version 3.1, the application object
constructor will be called before any of the Turbo Vision library
global objects are built.
This poses a problem because certain functions that are called
when the application object is built require that objects such as
TScreen be constructed first, and this won't happen because the
order of initialization of global objects has changed. If you
run such an application, there will be a short delay and then a
movable mouse cursor will appear on the screen. No Turbo Vision
screen will appear and you will be able to do very little.