www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/03/10:21:01

From: Robert Hoehne <Robert DOT Hoehne AT Mathematik DOT tu-chemnitz DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: [Q] Static class member variables
Date: Tue, 03 Jun 1997 13:16:50 +0200
Organization: TU Chemnitz-Zwickau
Lines: 52
Message-ID: <3393FD22.4485C9A3@Mathematik.tu-chemnitz.de>
References: <865334760 DOT 165475 AT red DOT parallax DOT co DOT uk>
NNTP-Posting-Host: pandora.hrz.tu-chemnitz.de
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019