Path: utzoo!utgpu!water!watmath!clyde!rutgers!cmcl2!nrl-cmf!ames!sdcsvax!ucsdhub!hp-sdd!hplabs!decwrl!sun!pitstop!sundc!seismo!uunet!mcvax!cernvax!hslrswi!robert From: robert@hslrswi.UUCP (J. Robert Ward) Newsgroups: comp.lang.c++ Subject: Ordering of initialisation of static classes in *different* files ? Keywords: static class initialisation ordering Message-ID: <734@hslrswi.UUCP> Date: 14 Jan 88 16:14:03 GMT Organization: Hasler AG, CH-3000 Berne 14, Switzerland Lines: 51 Hello C++ Gurus, It seems to me that there is a (small) defect in the definition of the C++ language. Namely, the order in which static objects are initialised is apparently undefined when more than one source file is under consideration. For example, consider the following file: # include struct foo { ostream *mystream ; // Internal value foo( ostream& s ) ; // Constructor with arg } ; foo::foo ( // The real constructor ostream& s ) { mystream = &s ; ... // Do something with the arg } foo foo1( cout ) ; // Use the constructor Question: how can I guarantee that cout is properly initialised by its constructor before it gets passed as an argument to the constructor foo::foo() ? Since both cout and foo1 are global objects, I presume the order of construction is undefined. Or am I missing something here ? The problem came about in trying to solve question 12 on page 167 of the C++ book. Can this problem really be solved assuming I am not allowed to change main() in any way ? Many thanks for any light shed on this problem, Cheers, - Robert. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ J. Robert Ward, ___________ Hasler AG, Belpstrasse 23, CH-3000 Berne 14, Switzerland | _ | | _| |_ | Tel.: +41 31 633922 | |_ _| | X.400: robert@hslrswi.hasler | |_| | Bitnet: robert%hslrswi.UUCP@cernvax.BITNET |_________| Uucp: ... {uunet,ukc,mcvax ... }!cernvax!hslrswi!robert ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~