static const member initialization

Discussion in 'C++' started by Spoon, Dec 30, 2006.

  1. Spoon

    Spoon Guest

    Hello everyone,

    I have a Packet class I use to send packets over the Internet. All the
    packets sent in a session are supposed to share a common random ID.

    I figured I'd use a static const member inside my class.

    class Packet
    {
    static const int session_id;
    ...
    };

    My problem is: where (and when) do I initialize session_id.

    On my first try, I added the following statement in packet.cpp

    const int Packet::session_id = rand();

    But, as far as I understand, this initialization occurs before main()
    starts, thus, before I can call srand().

    Therefore, this implementation seems incorrect.

    I suppose I can use the so-called "construct-on-first-use" idiom
    from the C++ FAQ.

    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.16

    Are there other ways?
    (Remember: I'm supposed to call srand() only once.)

    Regards.
     
    Spoon, Dec 30, 2006
    #1
    1. Advertising

  2. Spoon

    John Carson Guest

    "Spoon" <root@localhost> wrote in message
    news:45968e9b$0$307$
    > Hello everyone,
    >
    > I have a Packet class I use to send packets over the Internet. All the
    > packets sent in a session are supposed to share a common random ID.
    >
    > I figured I'd use a static const member inside my class.
    >
    > class Packet
    > {
    > static const int session_id;
    > ...
    > };
    >
    > My problem is: where (and when) do I initialize session_id.
    >
    > On my first try, I added the following statement in packet.cpp
    >
    > const int Packet::session_id = rand();
    >
    > But, as far as I understand, this initialization occurs before main()
    > starts, thus, before I can call srand().
    >
    > Therefore, this implementation seems incorrect.
    >
    > I suppose I can use the so-called "construct-on-first-use" idiom
    > from the C++ FAQ.
    >
    > http://www.parashift.com/c -faq-lite/ctors.html#faq-10.16
    >
    > Are there other ways?


    How about:

    class Packet
    {
    static const int session_id;
    };

    int InitializeStatic(unsigned seed)
    {
    srand(seed);
    return rand();
    }

    const int Packet::session_id = InitializeStatic(5);


    --
    John Carson
     
    John Carson, Dec 30, 2006
    #2
    1. Advertising

  3. Spoon

    subramanian Guest

    Can a function be called in a static member initialization ? I thought
    static member initialization should be constants.
     
    subramanian, Dec 31, 2006
    #3
  4. Spoon

    Spoon Guest

    John Carson wrote:

    > Spoon wrote:
    >
    >>I have a Packet class I use to send packets over the Internet. All the
    >>packets sent in a session are supposed to share a common random ID.
    >>
    >>I figured I'd use a static const member inside my class.
    >>
    >>class Packet
    >>{
    >> static const int session_id;
    >> ...
    >>};
    >>
    >>My problem is: where (and when) do I initialize session_id.
    >>
    >>On my first try, I added the following statement in packet.cpp
    >>
    >>const int Packet::session_id = rand();
    >>
    >>But, as far as I understand, this initialization occurs before main()
    >>starts, thus, before I can call srand().
    >>
    >>Therefore, this implementation seems incorrect.
    >>
    >>I suppose I can use the so-called "construct-on-first-use" idiom
    >>from the C++ FAQ.
    >>
    >>http://www.parashift.com/c -faq-lite/ctors.html#faq-10.16
    >>
    >>Are there other ways?

    >
    > How about:
    >
    > class Packet
    > {
    > static const int session_id;
    > };
    >
    > int InitializeStatic(unsigned seed)
    > {
    > srand(seed);
    > return rand();
    > }
    >
    > const int Packet::session_id = InitializeStatic(5);


    Hello John,

    The problem with this solution is that I have other classes that call
    rand(). I don't want to make class Packet responsible for initializing
    the RNG. For example, if I don't use Packet in one of my programs, and
    if I also use the other classes, the RNG will always be seeded with 0.

    The solution I'm looking for is:
    call srand in main
    all static const members are initialized after that point.

    Does someone know how to do that?

    Regards.
     
    Spoon, Dec 31, 2006
    #4
  5. Spoon

    John Carson Guest

    "subramanian" <> wrote in message
    news:
    > Can a function be called in a static member initialization ?


    Yes.

    > I thought static member initialization should be constants.


    Why?


    --
    John Carson
     
    John Carson, Dec 31, 2006
    #5
  6. Spoon

    John Carson Guest

    "Spoon" <root@127.0.0.1> wrote in message
    news:45977bea$0$323$
    > John Carson wrote:
    >
    >> Spoon wrote:
    >>
    >>> I have a Packet class I use to send packets over the Internet. All
    >>> the packets sent in a session are supposed to share a common random
    >>> ID. I figured I'd use a static const member inside my class.
    >>>
    >>> class Packet
    >>> {
    >>> static const int session_id;
    >>> ...
    >>> };
    >>>
    >>> My problem is: where (and when) do I initialize session_id.
    >>>
    >>> On my first try, I added the following statement in packet.cpp
    >>>
    >>> const int Packet::session_id = rand();
    >>>
    >>> But, as far as I understand, this initialization occurs before
    >>> main() starts, thus, before I can call srand().
    >>>
    >>> Therefore, this implementation seems incorrect.
    >>>
    >>> I suppose I can use the so-called "construct-on-first-use" idiom
    >>> from the C++ FAQ.
    >>>
    >>> http://www.parashift.com/c -faq-lite/ctors.html#faq-10.16
    >>>
    >>> Are there other ways?

    >>
    >> How about:
    >>
    >> class Packet
    >> {
    >> static const int session_id;
    >> };
    >>
    >> int InitializeStatic(unsigned seed)
    >> {
    >> srand(seed);
    >> return rand();
    >> }
    >>
    >> const int Packet::session_id = InitializeStatic(5);

    >
    > Hello John,
    >
    > The problem with this solution is that I have other classes that call
    > rand(). I don't want to make class Packet responsible for initializing
    > the RNG. For example, if I don't use Packet in one of my programs, and
    > if I also use the other classes, the RNG will always be seeded with 0.
    >
    > The solution I'm looking for is:
    > call srand in main
    > all static const members are initialized after that point.
    >
    > Does someone know how to do that?


    It can't be done.

    A version of "construct-on-first-use", as you previously suggested, would
    seem to be your best bet, e.g.,

    class Packet
    {
    static const int session_id;
    };

    int InitializedRand(unsigned seed=0)
    {
    static bool needInitialization = true;
    if(needInitialization)
    {
    srand(seed);
    needInitialization = false;
    }
    return rand();
    }

    const int Packet::session_id = InitializedRand(5);

    In use, you would call InitializedRand in place of your first call to rand()
    for the initialization of *each* static variable that requires it.

    In case there aren't any static variables requiring rand(), you would also
    use it in place of your first call to rand() from main() (which is most
    simply accomplished by always calling it at the start of main() ). As an
    alternative to this call from main, you might choose to have an object that
    is a static variable in every program that you write --- an "application"
    object --- and call the function in its initialization.


    --
    John Carson
     
    John Carson, Dec 31, 2006
    #6
  7. Spoon

    Spoon Guest

    John Carson wrote:

    > subramanian wrote:
    >
    >> Can a function be called in a static member initialization ?

    >
    > Yes.
    >
    >> I thought static member initialization should be constants.

    >
    > Why?


    In C, AFAIU, objects with static duration must be initialized with
    static initializers, i.e. expressions that the translator can evaluate
    prior to program startup.

    http://www-ccs.ucsd.edu/c/declare.html
     
    Spoon, Dec 31, 2006
    #7
  8. Spoon

    John Carson Guest

    "Spoon" <root@127.0.0.1> wrote in message
    news:4597be65$0$296$
    > John Carson wrote:
    >
    >> subramanian wrote:
    >>
    >>> Can a function be called in a static member initialization ?

    >>
    >> Yes.
    >>
    >>> I thought static member initialization should be constants.

    >>
    >> Why?

    >
    > In C, AFAIU, objects with static duration must be initialized with
    > static initializers, i.e. expressions that the translator can evaluate
    > prior to program startup.
    >
    > http://www-ccs.ucsd.edu/c/declare.html



    It makes sense that C++ would not follow C in this respect. Objects of class
    type all have constructors to initialize them. Thus if any object of class
    type is static, it is initialized by a function (i.e., the constructor) even
    where no function is explicitly called.

    It would be very inconvenient if the permitted form of initialization for
    class objects differed depending on whether they were static variables.

    --
    John Carson
     
    John Carson, Dec 31, 2006
    #8
    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. Rakesh Sinha
    Replies:
    4
    Views:
    1,858
    Rakesh Sinha
    Jan 13, 2005
  2. Dave
    Replies:
    10
    Views:
    35,334
    Ron Natalie
    May 22, 2005
  3. Javier
    Replies:
    2
    Views:
    584
    James Kanze
    Sep 4, 2007
  4. Replies:
    23
    Views:
    881
    James Kanze
    Jun 15, 2008
  5. fungus
    Replies:
    13
    Views:
    901
    fungus
    Oct 31, 2008
Loading...

Share This Page