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!

2

4 Answers 4

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.

4
  • The dump thing is in "GUIDELINE" from instructor said that declare numEmployees as a static integer value of 0 in private member of class Commented May 20, 2012 at 8: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. Commented May 20, 2012 at 8:48
  • Never mind, I can accept that, "homework" make me feel good 'cause I dont have any hw for longtime. OK Commented May 20, 2012 at 8:55
  • @CodyGray thanks for pointing that out, I never actually read the whole story behind the homework tag.
    – stijn
    Commented May 20, 2012 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.

0
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.

3
  • I meant in class definition.put current declaration without const in public access.
    – ravi
    Commented May 20, 2012 at 8:34
  • The dump thing is in "GUIDELINE" from instructor said that declare numEmployees as a static integer value of 0 in private member of class Commented May 20, 2012 at 8:37
  • static member is initialized with 0 always
    – ravi
    Commented May 20, 2012 at 8:41
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.

1
  • The dump thing is in "GUIDELINE" from instructor said that declare numEmployees as a static integer value of 0 in private member of class Commented May 20, 2012 at 8:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.