Mail Archives: djgpp/1997/06/03/10:21:01
John Eccleston wrote:
>
> 'The C++ Programming Language' 2ed by Bjarne Stroustrup is valid C++ syntax
I'm not a C++ expert, but the following works and should have the same
effect.
-------> Example <-------
#include <stdio.h>
class clsTest
{
public :
static int instanceCount;
public :
clsTest() { instanceCount++; };
~clsTest() { instanceCount--; };
virtual int getInstanceCount(void) { return instanceCount; };
};
int clsTest::instanceCount = 0;
main(int argc, char *argv[])
{
clsTest
*test;
printf("Instances (before) %d\n", clsTest::instanceCount);
test = new clsTest();
printf("Instances: %d\n", test->getInstanceCount());
delete test;
}
----> End of Example <---
> Could anybody explain what is happening here. I have looked in the FAQ
> but couldn't spot anything.
I think, gcc (g++) treats your
static int instanceCount = 0;
inside the class definition only as definition of the static
member and not as declaration.
Please do not ask what correct is, I don't know.
Robert
--
*****************************************************************
* Robert Hoehne, Fakultaet fuer Mathematik, TU-Chemnitz-Zwickau *
* Post: Am Berg 3, D-09573 Dittmannsdorf *
* e-Mail: Robert DOT Hoehne AT Mathematik DOT TU-Chemnitz DOT DE *
* WWW: http://www.tu-chemnitz.de/~rho *
*****************************************************************
- Raw text -