delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/06/17:32:37

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: [Q] Static class member variables
Date: Fri, 06 Jun 1997 01:56:02 -0700
Organization: Alcyone Systems
Lines: 42
Message-ID: <3397D0A2.3E63F481@alcyone.com>
References: <865334760 DOT 165475 AT red DOT parallax DOT co DOT uk>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

John Eccleston wrote:

> #include <stdio.h>
> 
> class clsTest
> {
> public :
>    static int instanceCount = 0;
> 
> public :
>    clsTest() { instanceCount++; };
>    ~clsTest() { instanceCount--; };
> 
>    virtual int getInstanceCount(void) { return instanceCount; };
> };

This is not legal; you cannot give initializers for static members inside
the class declaration.  (Such member declarations are _declarations_, not
definitions.)

You need to declare the static member and then define it separately to
avoid the errors:

    class clsTest
    {
      public:
        static int instanceCount;

        // ...
    };

    int clsTest::instanceCount = 0;

    // ...

-- 
       Erik Max Francis, &tSftDotIotE / email / max AT alcyone DOT com
                     Alcyone Systems /   web / http://www.alcyone.com/max/
San Jose, California, United States /  icbm / 37 20 07 N  121 53 38 W
                                   \
     "Covenants without the sword / are but words."
                                 / Camden

- Raw text -


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