Path: utzoo!utgpu!news-server.csri.toronto.edu!eecg.toronto.edu!zq Newsgroups: comp.lang.c++ From: zq@eecg.toronto.edu (Qing Zheng) Subject: Buggs in AT&T C++ 2.0 ? Message-ID: <1991Jan12.174410.9491@jarvis.csri.toronto.edu> Distribution: comp Date: 12 Jan 91 22:44:10 GMT Lines: 118 Hi, I found the initialization sequence of classes with static member variables is depended on the link sequence. At the end of this mail. I include a shell archive containing three files : base.h base.C, and main.C. They will show the case I mentioned above. I'm using AT&T C++ 2.0 under SunOS 3.5. If I compile this way : %CC main.C base.C %a.out The output is : myInt::myInt() base::base() No = 1 base::base() No = 2 No = 2 However, If I compile the other way, %CC base.C main.C %a.out The output is base::base() No = 1 base::base() No = 2 myInt::myInt() No = 0 Why are the two outputs different ? Qing zq@eecg.toronto.edu P.S. If you combine these three files into one file, there is no problem. -------------CUT HERE ---------- #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # If this archive is complete, you will see the following message at the end: # "End of shell archive." # # Contents: # base.h base.C main.C # # Wrapped by zq@eecg.toronto.edu on Sun Dec 23 18:20:23 1990 # PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f base.h -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"base.h\" else echo shar: Extracting \"base.h\" \(299 characters\) sed "s/^X//" >base.h <<'END_OF_base.h' X#include X Xstruct myInt { X int no; X myInt(); X void inc() { no++; } X void print() { cerr << "No = " << no << "\n"; } X}; X Xclass base { Xprivate : X static myInt number_of_objects; Xpublic : X static void print() { number_of_objects.print(); } X base(); X}; END_OF_base.h if test 299 -ne `wc -c base.C <<'END_OF_base.C' X#include X#include "base.h" X XmyInt::myInt() { cerr << "myInt::myInt()\n"; no = 0; } X XmyInt base::number_of_objects; Xbase::base() X{ X number_of_objects.inc(); X cerr << "base::base() "; X print(); X} END_OF_base.C if test 218 -ne `wc -c main.C <<'END_OF_main.C' X#include "base.h" X Xbase a, b; X Xmain() X{ X base::print(); X} END_OF_main.C if test 61 -ne `wc -c