[comp.lang.c++] Problem with 2.0 Iostream_init

tom@runxtsa.runx.oz.au (Thomas Antalffy) (08/21/90)

THE TASK:

    I have to rewrite C++ 2.0 streams to work under Microsoft Windows,
    i.e: write .c files that can be compiled for MS Windows to match the
AT&T .h-s.  This should be fairly straightforward given the fact that I
have done the same for C++ 1.2 streams.

THE PROBLEM:

    2.0 has a class Iostream_init that initializes cin, cout, cerr and
    clog.  At any stage in globally scoped objects the Iostream_init
constructor can be called to ensure that predefined streams are working.
In other words Iostream_init::Iostream_init() has to initialize cin,
cout, cerr and clog.

    Lets take cout as an example.  In <iostream.h> (where Iostream_init
    is declared) cout is declared as extern ostream_withassign.  The
only methods of ostream_withassign are ostream_withassign(void),
~ostream_withassign(void), operator=(ostream), operator=(streambuf).  In
other words the only way to create an ostream_withassign associated with
a certain file descriptor is to create a 'blank' one with the
constructor, and then assign an ostream opened on that file descriptor
to it.

    So, how can Iostream_init::Iostream_init() assign to cout, given
    that cout might well not be initialized (constructed) at the time
(the order of static constructions is only guarantied within one file) ?
I tried assigning to cout while it was uninitialized, and had all kinds
of trouble - basically 'this' ended up being NULL.

    Alternatively how can one force the initialization of a variable
    declared as extern, and ensure that when the real static constructor
comes around, it does not mess up the values ?

    Or putting it another way, does anyone have any idea how C++ 2.0
    does it ?