0

I have written the following sample code :

class MyClass {
    static int a;
  public:
    MyClass ( int i ) : a ( i )    {
      cout << " \n ctor called. a is : "<< a << " \n";
    }
};

int   MyClass::a = 1;   

int main( ) {
    MyClass my(2);
}

I know this will give a compilation error as a static data member cannot be used in the constructor initializer list.

So how to initialize the static data member each time an object of the class is created ? I hope a static member function invoked from constructor can do it. Is that the only possible way?

nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
  • 2
    That makes no sense. If you need something "for each object of the class", then it's not static. – Kerrek SB Dec 09 '11 at 10:37
  • You do realise that your static int has the same value across all instances of MyClass? – ChrisBD Dec 09 '11 at 10:37
  • @Kerresk: I know only one copy of the static data member will exist. But i need to assign a value to it each time an object is created, so that the previously created objects also share the same value. Does that make sense? – nitin_cherian Dec 09 '11 at 10:44
  • Now you say "assign", while in your question you say "initialize". This is confusing. Attention to detail is important when dealing with technical intricacies. – Kerrek SB Dec 09 '11 at 10:47

4 Answers4

5

Simple, assign a value to a in the body of the constructor:

MyClass ( int i )  {
  a = i;
  cout << " \n ctor called. a is : "<< a << " \n";
}

You can't use an initializer list to do so because a isn't being initialized, it's simply being assigned a new value. Before the constructor was called, at the start of the program, a will be initialized to the value 0 (uninitialized static variables are set to 0).

This is what the compiler tells you, which is fairly straight-forward:

error: ‘int MyClass::a’ is a static data member; it can only be initialized at its definition

Seems kind of silly though, you sure it should be static and not an instance variable? Only one instance of the static variable exists throughout your program and has a lifetime from the beginning of the program's execution to the termination of the program.

EDIT: It seems you do realise that there will only be one instance of the static variable, by the looks of one of your comments. Just note that initialization and assignment are two different things.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
2

The keyword static states that your member variable will not have its own value for each class instance but one value that is being initialized with the line of code you wrote:

int   MyClass::a = 1;

The goal of the constructor is to initialize an instance, but static variable members have no relation to instances so you can't initialize them in an initialization list, which is used to initialize non-static members. If you assign a value to your variable not in the initialization list of your constructor, you'll simply replace the value of your variable since it is static.

You should read more about it here: http://www.bogotobogo.com/cplusplus/statics.php

phhkafot
  • 183
  • 1
  • 10
1

Static member variables are supposed to stay the same for all instances of a class. If you want to change it in the constructor use a normal member variable.

But to answer your question: Yes, you can to call a static member function to be able to set the variable. Or just set it in the body of the function, and not in the initializer list.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

How to initialize the static data member each time an object of the class is created?

You cannot do this. Static data member is not a part of the object/instance of the class but is object on its own, created and initialized only once, typically when program starts. What you can do on creation of a new class instance is to assign a new value to it. (But the question is why would you do this - use non-static member that should be instance-specific; static members are shared among all instances of class).

Non-const static int member must be defined at a file scope and that's the place where you can initialize it explicitly, as you did in your example. If you omitted initializer (1) your variable, as it has static duration, would have been initialized by compiler with 0.

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51