[net.lang.c++] Constructor execution ordering between modules.

stephen@comp.lancs.ac.uk (Stephen J. Muir) (08/12/86)

Hi.  This is my first posting to this newsgroup.  I have been reading the book
by Bjarne Stroustrup, and decided to try problem 12 at the end of chapter 5.
For those of you without the book, here is the problem:

Given the program:

# include <stream.h>

main ()
{
	cout << "Hello, world\n";
}

modify it to produce the output

Initialise
Hello, world
Clean up

Do not change main() in any way.

Easy, I thought:

# include <stream.h>

class x
	{ char	y;
public:
	  x()	{ cout << "Initialize\n"; }
	  ~x()	{ cout << "Clean up\n"; }
	};

x z;

main ()
{
	cout << "Hello, world\n";
}

Unfortunately, all I get is:

Segmentation fault (core dumped)

When I remove the constructor, I correctly get the last two lines.  It seems
that I was trying to use "cout" before it was initialised.  Is this a bug, or
am I missing something?
-- 
EMAIL:	stephen@comp.lancs.ac.uk	| Post: University of Lancaster,
UUCP:	...!mcvax!ukc!dcl-cs!stephen	|	Department of Computing,
Phone:	+44 524 65201 Ext. 4120		|	Bailrigg, Lancaster, UK.
Project:Alvey ECLIPSE Distribution	|	LA1 4YR