🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #1582

When to construct static objects

#1582
From: David Fox
Date: Fri, 30 Dec 1988 23:15
46 lines
1371 bytes
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?
Technical Details
Message-ID: <63@cs.columbia.edu>
Path: arc1-ca.pugleaf.net!utzoo!telly!ddsw1!lll-winken!killer!mit-eddie!bloom-beacon!apple!rutgers!columbia!cs!fox

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!telly!ddsw1!lll-winken!killer!mit-eddie!bloom-beacon!apple!rutgers!columbia!cs!fox
From: fox@cs.columbia.edu (David Fox)
Newsgroups: gnu.g++.bug
Subject: When to construct static objects
Message-ID: <63@cs.columbia.edu>
Date: 30 Dec 88 23:15:36 GMT
Reply-To: fox@cs.columbia.edu (David Fox)
Distribution: gnu
Organization: Columbia University Department of Computer Science
Lines: 46