About static const members appearing in another static const definitions

Discussion in 'C++' started by Rakesh Sinha, Jan 13, 2005.

  1. Rakesh Sinha

    Rakesh Sinha Guest

    I am having this code here.


    static const float PI = 3.14159;
    static const float INC = 0.4f * PI;

    When I compile my program, I get the following error,

    error: `MyClass::pI' cannot appear in a constant-expression .

    I was wondering why this is not possible here. May be is it because
    there is no guaranteed order of initialization among C++ members ?

    But when, intuitively, I had defined PI here, and then use PI later in
    a subsequent definition. Would it not be possible to use PI
    here,(since i can't change PI anymore ) ?
     
    Rakesh Sinha, Jan 13, 2005
    #1
    1. Advertising

  2. "Rakesh Sinha" <> wrote...
    >I am having this code here.


    Where?

    > static const float PI = 3.14159;
    > static const float INC = 0.4f * PI;


    Only static data members of integral types can be initialised inside
    the class definition. Please in the future don't make us guess or
    read your mind. As much as we can do it successfully, it still takes
    a lot of energy, which we have a very limited amount to spare.

    > When I compile my program, I get the following error,
    >
    > error: `MyClass::pI' cannot appear in a constant-expression .
    >
    > I was wondering why this is not possible here. May be is it because
    > there is no guaranteed order of initialization among C++ members ?


    No, the order of initialisation is well-defined. It's simply not
    allowed to initialise a floating point member in the class definition.

    > But when, intuitively, I had defined PI here, and then use PI later in
    > a subsequent definition. Would it not be possible to use PI
    > here,(since i can't change PI anymore ) ?
    >


    Here where?

    Pull your definitions out of the class (drop the 'static' keyword, of
    course), and they should be working just fine.

    Victor
     
    Victor Bazarov, Jan 13, 2005
    #2
    1. Advertising

  3. Rakesh Sinha

    Mike Wahler Guest

    "Victor Bazarov" <> wrote in message
    news:...
    > "Rakesh Sinha" <> wrote...
    > >I am having this code here.

    >
    > Where?
    >
    > > static const float PI = 3.14159;
    > > static const float INC = 0.4f * PI;

    >
    > Only static data members of integral types can be initialised inside
    > the class definition. Please in the future don't make us guess or
    > read your mind. As much as we can do it successfully, it still takes
    > a lot of energy, which we have a very limited amount to spare.
    >
    > > When I compile my program, I get the following error,
    > >
    > > error: `MyClass::pI' cannot appear in a constant-expression .
    > >
    > > I was wondering why this is not possible here. May be is it because
    > > there is no guaranteed order of initialization among C++ members ?

    >
    > No, the order of initialisation is well-defined. It's simply not
    > allowed to initialise a floating point member in the class definition.
    >
    > > But when, intuitively, I had defined PI here, and then use PI later in
    > > a subsequent definition. Would it not be possible to use PI
    > > here,(since i can't change PI anymore ) ?
    > >

    >
    > Here where?
    >
    > Pull your definitions out of the class (drop the 'static' keyword, of
    > course), and they should be working just fine.


    Rakesh:

    To make sure you understand what Victor means:

    Retain the 'static' in your declaration in the class body,
    but omit it in the definition, like this:

    class C
    {
    const static float PI;
    };

    const float C::pI(3.14159);


    However, I recommend you change your type to 'double'
    which has much greater range and precision, which
    will give better accuracy in your floating point
    calculations.

    -Mike
     
    Mike Wahler, Jan 13, 2005
    #3
  4. Re: About static const members appearing in another static constdefinitions

    Rakesh Sinha wrote:
    > I am having this code here.
    >
    >
    > static const float PI = 3.14159;
    > static const float INC = 0.4f * PI;
    >
    > When I compile my program, I get the following error,
    >
    > error: `MyClass::pI' cannot appear in a constant-expression .


    It is currently illegal to use non integral
    initializers in the class' definition. Define it
    elsewhere.

    // my_math.h
    class Math
    {
    public:
    static const double PI;
    };

    // my_math.cpp
    const double Math::pI = 3.1416;

    See 9.4.2.4:
    If a static data member is of const integral
    or const enumeration type, its declaration in the
    class definition can specify a
    constant-initializer which shall be an
    integral constant expression (_expr.const_).

    > I was wondering why this is not possible here.


    A quick google search gave

    http://groups.google.ca/groups?selm=

    > May be is it because
    > there is no guaranteed order of initialization among C++ members ?


    No, in this case, the order is defined. PI gets
    initialized before INC.


    Jonathan
     
    Jonathan Mcdougall, Jan 13, 2005
    #4
  5. Rakesh Sinha

    Rakesh Sinha Guest

    But specifying 'static const float PI = 3.14159f;' seems to compile
    fine on my implementation (gnu c/c++ 3.41 - I know that does not mean
    it is legal.) . Hence I did not even think that there is a problem
    there. Thanks everyone for pointing out the same.
     
    Rakesh Sinha, Jan 13, 2005
    #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. JFCM
    Replies:
    4
    Views:
    5,780
  2. Dave
    Replies:
    10
    Views:
    35,398
    Ron Natalie
    May 22, 2005
  3. Javier
    Replies:
    2
    Views:
    621
    James Kanze
    Sep 4, 2007
  4. er
    Replies:
    3
    Views:
    402
  5. Vladimir Menshakov
    Replies:
    1
    Views:
    386
Loading...

Share This Page