static const in class

This is a discussion on static const in class within the C++ Programming forums, part of the General Programming Boards category; Hi, This question may have been asked before, but I would like some further clarification. Is it always correct to ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    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
    21,941
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,740
    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
    21,941
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01: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, 03: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, 06:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21