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?