3

I have a class in header file:

class Employee
    {
        //Private data members
    private:
        string firstName;
        string lastName;
        char gender;

        //number of employees
        const static int numEmployees = 0;

    public: 
    ....
    };

The dumb thing is in "GUIDELINE" from instructor said that declare numEmployees as a static integer value of 0 in private member of class

Problem is I can't update numEmployees variable since it's const, for example when you declare Constructor in public: .. you can not increase numEmployees = numEmployees + 1.

If you don't declare numEmployees as const, just do static int numEmployees; visual studio 2010 give error said that only const will be declared in class.

Any idea how to declare numEmployees? Thank you!

Mike
  • 47,263
  • 29
  • 113
  • 177
Ronaldinho Learn Coding
  • 13,254
  • 24
  • 83
  • 110

4 Answers4

6

Since numEmployees is going to change, it should not be const. Non-const static variables have to be initialized outside of the class declaration, for example in the source file, like so:

int Employee::numEmployees = 0;

that being said, numEmployees being a member of the Employee class is probably not the best idea.

And I would make gender an enum, not a char.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
stijn
  • 34,664
  • 13
  • 111
  • 163
  • The dump thing is in "GUIDELINE" from instructor said that declare numEmployees as a static integer value of 0 in private member of class – Ronaldinho Learn Coding May 20 '12 at 08:36
  • @stijn We don't tag questions homework because that's what they *sound* like. Questions should only be tagged [homework] if someone is looking for homework-style answers (e.g., those that *hint* at the solution without providing actual working code). It's the asker's choice to tag his questions as (s)he pleases. Please do not try and force people to add this tag. – Cody Gray - on strike May 20 '12 at 08:48
  • Never mind, I can accept that, "homework" make me feel good 'cause I dont have any hw for longtime. OK – Ronaldinho Learn Coding May 20 '12 at 08:55
  • @CodyGray thanks for pointing that out, I never actually read the whole story behind the homework tag. – stijn May 20 '12 at 13:13
2

In C++, static variables needs to be declared in the class declaration but also defined in an implementation module.

// --- .h interface file
class MyClass
{
    public:
        static int my_static_variable;
    ...
};

// --- .cpp implementation file
#include "myclass.h"
int MyClass::my_static_variable = 0;

There is no real technical argument for this limitation, but it's how the language is defined.

If for some reason you really need to circumvent it, you can use function-level statics:

class MyClass
{
    public:
        // Note: returning a reference to int!
        static int& my_static_variable()
        {
            static int n = 0;
            return n;
        }
};

However, to access the variable, you will need in this case to formally call a method:

MyClass::my_static_variable() = 0;
MyClass::my_static_variable() ++;
MyClass::my_static_variable() *= 2;

I said "formally" because for such a simple function declared inline, the machine code generated by a decent compiler will be the same as the code needed to handle just a variable.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
6502
  • 112,025
  • 15
  • 165
  • 265
1

Initialize numEmployees outside of the class as

Employee::numEmployees = 0

and declare it a public member in the class as

static int numEmployees;

You can also declare it private without intialization, as a static member is assigned zero by default.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
ravi
  • 3,304
  • 6
  • 25
  • 27
1

You have declared it as const. How can you change a const parameter ?

So you can have it as static and initialize to some value you want in the constructor. By default the static variables are initialized to zero.

Jay D
  • 3,263
  • 4
  • 32
  • 48