🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by David Fox Fri, 30 Dec 1988 23:15
When to construct static objects
#1582
Author: 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?
Re: When to construct static objects
#1583
Author: David Fox
Date: Fri, 30 Dec 1988 23:23
2 lines
565 bytes
By the way, the program was compiled with g++-1.31.0 running on a
Sun 3/60 under SunOS 3.4.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads