Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!HARVARD.HARVARD.EDU!ngo%tammy From: ngo%tammy@HARVARD.HARVARD.EDU (Tom Ngo) Newsgroups: gnu.g++.bug Subject: static variables get *defined* in class declaration (1.36.4) Message-ID: <9002232019.AA19535@life.ai.mit.edu> Date: 23 Feb 90 20:18:49 GMT Sender: daemon@tut.cis.ohio-state.edu Distribution: gnu Organization: GNUs Not Usenet Lines: 39 Machine: Convex C220 g++: 1.36.4 Problem: If a variable is declared static inside a class declaration, it gets defined once for each .cc file in which the class declaration gets #include'd. This causes the linker to complain about multiple definitions. Is this legal C++? If so, how can the compiler work out that there should be only one copy of the variable in question? Try g++ bar.cc bar2.cc -o bar, using the files below. =========================================================================== bar.h: #include class bar { protected: static int count = 0; public: void incr() { count++; } void show() { cout << count << "\n"; } }; =========================================================================== bar.cc: #include "bar.h" main() { bar foo,foo2; foo.show(); foo2.show(); foo.incr(); foo.show(); foo2.show(); } =========================================================================== bar2.cc: #include "bar.h" ===========================================================================