Thread: Why not static const double d = 1.5; inside a class?

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Why not static const double d = 1.5; inside a class?

    This line is accepted inside of a class by the VC9 compiler:

    Code:
    static const uint MAX_FRAME_ITERATIONS = 50;
    while this line is not:

    Code:
    static const double BOUNCE_COEFFICIENT = .8;
    When trying the latter, I got the error:

    Code:
    error C2864: 'physics::BOUNCE_COEFFICIENT' : only static const integral data members can be initialized within a class
    Instead I had to put it in the global scope, but that just feels stupid. Why doesn't it accept non-integer constants inside of a class?
    Come on, you can do it! b( ~_')

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because, I believe, the standard says so?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    So I have to keep the constant global?
    Come on, you can do it! b( ~_')

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unless I'm wrong, yes. We'll see soon enough
    Many experts on these things here
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I believe you need to do it like that:

    Code:
    struct X
    {
        static const double BOUNCE_COEFFICIENT;
    };
    
    const double X::BOUNCE_COEFFICIENT = 1.5;
    As to what exactly is the motivation, not sure. I don't know if there is any way to use a double value as a compile-time constant, so unlike integer types there doesn't seem any particular need.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You just need to initialize it outside the class:
    Code:
    const double physics::BOUNCE_COEFFICIENT = 0.8;
    The reason is that compilers are not required to have a floating point capable processors and some historically didn't even when the target platform would. So assigning an expression to a static variable would involve doing floating point operations that would be postponed to the start of a program in a manner similar to class constructors. The systax of initializing inside a class definition was intended to only be used for operations that the compiler could always do at compile time. That means it can only use integer expressions.

    The reason is dated, but the standard has not changed.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    I know that in some cases, the compiler can optimize the code if you use constants instead of variables. Is this true only for integer values as well maybe?
    Come on, you can do it! b( ~_')

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    No, these days compilers can use floating point constants just as easily as integers. The differences are for entirely historic reasons. Some of the differences, particularly concerning templates will be changed in C++0x.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    I can't declare the constant inside the class and define it outside either. Then I get the error:

    Code:
    error LNK2005: "private: static double const physics::BOUNCE_COEFFICIENT" (?BOUNCE_COEFFICIENT@physics@@0NB) already defined in ball.obj
    So I guess I have to make it a non-const anyway...
    Come on, you can do it! b( ~_')

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You have to define it in the implementation file, not in the header.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

Tags for this Thread