[gnu.g++.bug] When to construct static objects

fox@cs.columbia.edu (David Fox) (12/31/88)

The following program demonstrates the treatment g++ gives objects
which are declared static inside the scope of a function.  Each
time the function is entered the default constructor is called for
n.  To me, this seems to defeat the purpose of a static variable,
to retain its value from invocation to invocation.

	#include <stream.h>

	class Number {
	  int i;
	public:
	  Number() {i = 999;}
	  friend ostream& operator<<(ostream& s, Number& n)
	    {return s << n.i << "\n";}
	  set(int n) {i = n;}
	};

	func()
	{
	  static Number n;
	  cout << n;
	  n.set(555);
	  cout << n;
	}
 
	main()
	{
	  func();
	  func();
	}

This program prints

	999
	555
	999
	555

I guess I would call this a bug.

David Fox
fox@cs.columbia.edu

P.S. By the way, I have sent previous bug reports to bug-g++@prep.ai.mit.edu,
but they never appeared here.  Should I have?  Will I see this?  Will the
people who see bug-g++ see this?

fox@cs.columbia.edu (David Fox) (12/31/88)

By the way, the program was compiled with g++-1.31.0 running on a
Sun 3/60 under SunOS 3.4.