Is this allowed in c++?

Discussion in 'C++' started by Markus Ilmola, May 9, 2006.

  1. Is it really allowed to initialize a static class member by calling a
    function?

    class foo
    {
    public:
    static int bar;
    static float test;
    };

    int foo::bar = random_number();
    float foo::test = read_value_from_config_file();

    int main()
    {
    return 0;
    }


    It compiles fine with the GNU c++ compiler, but is it really allowed in
    the c++ standart? And more importantly is it a safe thing to do?


    All examples that a have found from the Internet only use predefined
    values like this:

    int foo::bar = 4;
    float foo::test = 2.0;
     
    Markus Ilmola, May 9, 2006
    #1
    1. Advertising

  2. "Markus Ilmola" wrote:
    > It compiles fine with the GNU c++ compiler, but is it really allowed in
    > the c++ standart? And more importantly is it a safe thing to do?
    >
    >
    > All examples that a have found from the Internet only use predefined
    > values like this:
    >
    > int foo::bar = 4;
    > float foo::test = 2.0;


    A static variable is a normal variable with a static duration (it is
    allocated when the program begins and deallocated when the program
    ends), so you can do whatever you want with it (Set it, reset it,
    reference it...)

    Abdo Haji-Ali
    Programmer
    In|Framez
     
    Abdo Haji-Ali, May 9, 2006
    #2
    1. Advertising

  3. Markus Ilmola

    Rolf Magnus Guest

    Markus Ilmola wrote:

    > Is it really allowed to initialize a static class member by calling a
    > function?


    Sure, why not? This is true for any variable with static storage duration.

    > class foo
    > {
    > public:
    > static int bar;
    > static float test;
    > };
    >
    > int foo::bar = random_number();
    > float foo::test = read_value_from_config_file();
    >
    > int main()
    > {
    > return 0;
    > }
    >
    >
    > It compiles fine with the GNU c++ compiler, but is it really allowed in
    > the c++ standart?


    Yes.

    > And more importantly is it a safe thing to do?


    It depends. If another static variable's constructor needs the value, you
    can run into problems due to initialization order.
     
    Rolf Magnus, May 9, 2006
    #3
  4. Markus Ilmola

    Guest

    , May 9, 2006
    #4
  5. Markus Ilmola

    Old Wolf Guest

    Rolf Magnus wrote:
    > Markus Ilmola wrote:
    >
    >> Is it really allowed to initialize a static class member by calling a
    >> function?

    >
    > Sure, why not?


    It isn't in C. There are plenty of good reasons why not, if you want
    to have an OT debate :)
     
    Old Wolf, May 10, 2006
    #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. Gary
    Replies:
    2
    Views:
    1,302
    Jeffrey Tan[MSFT]
    Jan 17, 2004
  2. Replies:
    1
    Views:
    3,451
    Michael Mayer [C# MVP]
    Feb 16, 2004
  3. HK
    Replies:
    1
    Views:
    3,713
    Cowboy \(Gregory A. Beamer\)
    Apr 1, 2004
  4. Rocio
    Replies:
    0
    Views:
    439
    Rocio
    Apr 5, 2004
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    551
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page