Thread: static const in class

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    8

    static const in class

    Hi,

    This question may have been asked before, but I would like some further clarification. Is it always correct to declare a constant this way?
    Code:
    class A
    {
        private:
               static const float var;
    
    }
    
    const float A::var = 1.2f;
    What I want is:
    1) a constant that can be used by class A member functions
    2) only one copy is created
    3) Basically to replace #define in C.

    I know you can initialize const integral in class, but I am looking for a standard way to declare const var that can be used for array/float etc.

    Thanks.
    Last edited by Champion; 10-25-2011 at 04:54 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is correct for a static const variable.
    Note that if you intend to replace #define, you should avoid static. The compiler is smart enough to understand that, unless you do something funny with the constants, like take their address, it can optimize them away and replace any references to that constant with its value instead.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    Note that if you intend to replace #define, you should avoid static. The compiler is smart enough to understand that, unless you do something funny with the constants, like take their address, it can optimize them away and replace any references to that constant with its value instead.
    Except in special cases, I don't think compilers can perform constant propagation for non-static const member variables because the value of such member variables may still vary at run time (the time of object creation), though it is constant for the lifetime of each object.

    If the member is intended to be a class constant rather than a per-object constant, declare it static const.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    8
    Quote Originally Posted by Elysia View Post
    This is correct for a static const variable.
    Note that if you intend to replace #define, you should avoid static.
    So would using static prevent the compiler from optimizing it out? Thanks.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This might be a tricky case as Laserlight points out. If you really want it be a constant, try placing it outside the class (you could put it in a namespace, perhaps).
    Otherwise looking at the assembly is really the only way to know.
    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.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Champion View Post
    So would using static prevent the compiler from optimizing it out? Thanks.
    No, if it's a class constant, static actually makes sense, and encourages optimizations based on constness. Constant member data that could be different among instances of the class cannot be made static, obviously. It is a lot harder to optimize some non-static constant out. That is how I read laserlight's post.

    Of course, Elysia is right that namespaces provide a good solution, as well, if it's OK that the constant isn't member data anymore.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    If you really want it be a constant, try placing it outside the class (you could put it in a namespace, perhaps).
    In my opinion, if a compiler can prove that constant propagation can be applied to a const qualified variable in namespace scope, then it can also prove that constant propagation can be applied to a static const member variable used in the same way as that const qualified variable in namespace scope.

    Once again, if you want a class constant, declare it as a static const member of the class. If you want a namespace level constant, declare it as a constant in namespace scope. Do not reach for one or the other as a premature optimisation that might not be an optimisation at all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to initialize static const array member of a class
    By nimitzhunter in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2010, 02:15 AM
  2. Replies: 4
    Last Post: 08-29-2010, 04:33 PM
  3. Why not static const double d = 1.5; inside a class?
    By TriKri in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2009, 04:01 PM
  4. Initializing static const memebers in a class
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2007, 05:39 PM
  5. Problems with static const class members
    By sigfriedmcwild in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2004, 07:57 AM