On Jul 25, 1:39*pm, mlimber <mlim...@gmail.comwrote:
On Jul 25, 1:31*pm, dani...@mail.ru wrote:
Hello there.
I have an abstract Base class with bunch of private static members and
public static getters for those members.
I want my Derived classes to call a static constructor and initialize
those members. How can this be achieved? Thank you.
Use a protected static initializer function:
*class Base
*{
* *static int i;
*protected:
* *static void Init( const int j ) { i = j; }
*public:
* *static int GetI() { return i; }
*};
*int Base::i = 0;
*class Derived : public Base
*{
*public:
* *Derived() { Init( 42 ); }
*};
Cheers! --M
Thank you for your quick response. The version that you gave me does
work, however, as you can see, the static Init() gets called every
time a new instance is being created. To me, it defeats the whole
purpose of the object being static in the first place. Is there a way
for me to call the static Init() only once? I tried to put
"Derived::Init()" but that didn't compile.