Newsgroups: comp.sys.mac.programmer
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!uupsi!srchtec.searchtech.com!davidr
From: davidr@searchtech.com (David Resnick)
Subject: problem with global object construction
Message-ID: <1991Apr23.223800.21774@searchtech.com>
Organization: search technology, inc.
Date: Tue, 23 Apr 1991 22:38:00 GMT

I'm using MPW 3.1, programming in C++.  I seem to have discovered that
if I define a global object, the constructor for that object is
not actually called until a non-inline method is called on the object
from within a block.  Here is my test program:

		#include <OSUtils.h>

		class FirstClass
		{
			public:
				FirstClass(void);	// the constructor

				// void beep(void) {}	// inline definition
				void beep();		// non-inline version
		};

		FirstClass :: FirstClass(void)
		{
			SysBeep(1000);
		}

		void FirstClass :: beep(void)		// non-inline definition
		{
		}

		FirstClass Yacht;			// <silence>

		void main()
		{
			// FirstClass Porsche;		// BEEP!

			Yacht.beep();			// BEEP!
		}

Running this program, exactly as is, results in a single system beep.  If
I use the inline definition of beep() instead of the non-inline definition,
I don't get a beep.  If I go back to the non-inline definition of beep()
and comment out the "Yacht.beep();" in main(), again there is no beep
(the FirstClass constructor does not execute during execution of the
"FirstClass Yacht;" global definition).  If I uncomment the
"FirstClass Porshe;" (and keep "Yacht.beep()" commented out) I get
one beep (since Porsche is defined within a block, the constructor executes
when Porshe is defined).

This seems somewhat unpleasant; the only way to get the constructor to
actually execute on a global object is to call a non-inline method of the
object you are trying to construct.  The other C++ compiler I've used doesn't
work this way (gnu C++).  Is this a known limitation of MPW C++ that I should
just try to live with, or am I missing something interesting?


-- 
David E. Resnick                                davidr@srchtec.uucp (registered)
search technology, inc.				           davidr@searchtech.com
4725 peachtree corners cir., suite 200		   {uupsi,stiatl}!srchtec!davidr
norcross, georgia 30092				                  (404) 441-1457
