zq@eecg.toronto.edu (Qing Zheng) (01/13/91)
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 <stream.h>
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.h`; then
echo shar: \"base.h\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f base.C -a "${1}" != "-c" ; then
echo shar: Will not over-write existing file \"base.C\"
else
echo shar: Extracting \"base.C\" \(218 characters\)
sed "s/^X//" >base.C <<'END_OF_base.C'
X#include <stream.h>
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 <base.C`; then
echo shar: \"base.C\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f main.C -a "${1}" != "-c" ; then
echo shar: Will not over-write existing file \"main.C\"
else
echo shar: Extracting \"main.C\" \(61 characters\)
sed "s/^X//" >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 <main.C`; then
echo shar: \"main.C\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0