Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Static Initialization of Base Class' member

Reply
Thread Tools

Static Initialization of Base Class' member

 
 
danil52@mail.ru
Guest
Posts: n/a
 
      07-25-2008
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.
 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      07-25-2008
On Jul 25, 1:31*pm, (E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
danil52@mail.ru
Guest
Posts: n/a
 
      07-25-2008
On Jul 25, 1:39*pm, mlimber <(E-Mail Removed)> wrote:
> On Jul 25, 1:31*pm, (E-Mail Removed) 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.
 
Reply With Quote
 
Darío Griffo
Guest
Posts: n/a
 
      07-25-2008
On Jul 25, 2:57*pm, (E-Mail Removed) wrote:
> On Jul 25, 1:39*pm, mlimber <(E-Mail Removed)> wrote:
>
>
>
> > On Jul 25, 1:31*pm, (E-Mail Removed) 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.


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

this Init is called only once
 
Reply With Quote
 
Joe Greer
Guest
Posts: n/a
 
      07-25-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:dae40744-516d-4e3f-b166-
(E-Mail 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
error C2614: 'CYYYRegister' : illegal member initialization:'CRequest' is not a base or member Angus C++ 1 03-06-2008 11:38 AM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
How to initialize an array member in the member initialization list? jut_bit_zx@eyou.com C++ 3 10-10-2005 12:10 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM



Advertisments