Static Initialization of Base Class' member

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • danil52@mail.ru

    Static Initialization of Base Class' member

    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.
  • mlimber

    #2
    Re: Static Initialization of Base Class' member

    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

    Comment

    • danil52@mail.ru

      #3
      Re: Static Initialization of Base Class' member

      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.

      Comment

      • =?ISO-8859-1?Q?Dar=EDo_Griffo?=

        #4
        Re: Static Initialization of Base Class' member

        On Jul 25, 2:57 pm, dani...@mail.ru wrote:
        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.
        Base::Init
        {
        static bool first = true;
        if(first)
        {
        first = false;
        //do your stuff WARNING NOT THREAD SAFE
        }
        }

        this Init is called only once

        Comment

        • Joe Greer

          #5
          Re: Static Initialization of Base Class' member

          danil52@mail.ru wrote in news:dae40744-516d-4e3f-b166-
          de7a737f89ef@x2 9g2000prd.googl egroups.com:
          >
          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::initi alized = false;


          You might also consider a singleton instead of statics.

          joe

          Comment

          Working...