🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

static variables get *defined* in class declaration (1.36.4)

#2213
From: Tom Ngo
Date: Fri, 23 Feb 1990 20:18
39 lines
1579 bytes
    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 <stream.h>
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"
===========================================================================
Technical Details
Message-ID: <9002232019.AA19535@life.ai.mit.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!HARVARD.HARVARD.EDU!ngo%tammy

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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