Path: 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 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 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?