Static Initialization of Base Class' member

D

danil52

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

mlimber

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
 
D

danil52

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

Darío Griffo

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.

Base::Init
{
static bool first = true;
if(first)
{
first = false;
//do your stuff WARNING NOT THREAD SAFE
}
}

this Init is called only once
 
J

Joe Greer

(e-mail address removed) wrote in (e-mail address removed):
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.

If you require dynamic initialization, you can't help it being called, but
you can protect it with a boolean that gets set to true after the init()
call finishes. Like so:

class Whatever {
static bool initialized;
static void Init()
{
if (!initialized)
{
// init stuff
initialized = true;
}
}

};

bool Whatever::initialized = false;


You might also consider a singleton instead of statics.

joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148

Latest Threads

Top