[comp.sys.mac.programmer] problem with global object construction

davidr@searchtech.com (David Resnick) (04/24/91)

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

lsr@Apple.COM (Larry Rosenstein) (04/26/91)

In article <1991Apr23.223800.21774@searchtech.com> davidr@searchtech.com (David Resnick) writes:
>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

Actually, the problem is that the code that performs the static object
initializations gets stripped by the linker if there's no reference to any
of the global objects.  If you were to add a second object and refer to it,
then the constructors for both objects would be called.

Your inline code didn't refer to the object, which is why using the inline
implementation didn't work.  If you had referred to the object in some way,
then it would have (I think).

This does seem like a bug to me.




-- 
Larry Rosenstein, Apple Computer, Inc.

lsr@apple.com
(or AppleLink: Rosenstein1)

lsr@Apple.COM (Larry Rosenstein) (04/30/91)

In article <1991Apr23.223800.21774@searchtech.com> davidr@searchtech.com (David Resnick) writes:
>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:

I got a message back from the C++ engineers.  There's a compiler flag -z5
which says to not strip unused static objects that have constructors or
destructors.  

-- 
Larry Rosenstein, Apple Computer, Inc.

lsr@apple.com
(or AppleLink: Rosenstein1)