ngo%tammy@HARVARD.HARVARD.EDU (Tom Ngo) (02/24/90)
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"
===========================================================================