Static Initialization of Base Class' member

Discussion in 'C++' started by danil52@mail.ru, Jul 25, 2008.

  1. Guest

    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.
     
    , Jul 25, 2008
    #1
    1. Advertising

  2. mlimber Guest

    On Jul 25, 1:31 pm, 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
     
    mlimber, Jul 25, 2008
    #2
    1. Advertising

  3. Guest

    On Jul 25, 1:39 pm, mlimber <> wrote:
    > On Jul 25, 1:31 pm, 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.
     
    , Jul 25, 2008
    #3
  4. On Jul 25, 2:57 pm, wrote:
    > On Jul 25, 1:39 pm, mlimber <> wrote:
    >
    >
    >
    > > On Jul 25, 1:31 pm, 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
     
    Darío Griffo, Jul 25, 2008
    #4
  5. Joe Greer Guest

    wrote in news:dae40744-516d-4e3f-b166-
    :

    >
    > 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
     
    Joe Greer, Jul 25, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Klatt
    Replies:
    6
    Views:
    2,699
    Jesper Madsen
    Nov 18, 2003
  2. Siemel Naran
    Replies:
    4
    Views:
    808
    Micah Cowan
    Jan 12, 2005
  3. , India
    Replies:
    2
    Views:
    395
    Tadeusz B. Kopec
    Nov 13, 2007
  4. dolphin
    Replies:
    3
    Views:
    1,349
    Pete Becker
    Dec 5, 2007
  5. Angus
    Replies:
    1
    Views:
    2,643
Loading...

Share This Page