const vs. static const inside class

Discussion in 'C++' started by John Goche, Dec 7, 2006.

  1. John Goche

    John Goche Guest

    Hello,

    I would like to know whethere there is a difference between
    a const variable and a static const variable inside a class.
    After all, if a variable is const in a class, the compiler can
    always optimize and turn it into a static const variable to
    save runtim memory/stack space since being const it
    cannot be changed. (?)

    Thanks,

    JG
    John Goche, Dec 7, 2006
    #1
    1. Advertising

  2. John Goche

    Mark P Guest

    John Goche wrote:
    > Hello,
    >
    > I would like to know whethere there is a difference between
    > a const variable and a static const variable inside a class.
    > After all, if a variable is const in a class, the compiler can
    > always optimize and turn it into a static const variable to
    > save runtim memory/stack space since being const it
    > cannot be changed. (?)
    >
    > Thanks,
    >
    > JG
    >


    A non-static constant member variable may have a different constant
    value for different instances of the class.
    Mark P, Dec 7, 2006
    #2
    1. Advertising

  3. John Goche wrote:
    > I would like to know whethere there is a difference between
    > a const variable and a static const variable inside a class.
    > After all, if a variable is const in a class, the compiler can
    > always optimize and turn it into a static const variable to
    > save runtim memory/stack space since being const it
    > cannot be changed. (?)


    I am not sure under what circumstances the non-static const data
    member can be optimized away. If the object is non-const to begin
    with, something can always cast away the const-ness and change the
    data member without much of a concern, really. So, the data member
    has to exist per instance. A static const data member exists per
    class, so it definitely can be placed in a const storage and using
    a const cast to remove const-ness on it would result in undefined
    behaviour. That's why it can actually be optimized by the compiler
    and be used as a literal in many cases. That's the difference,
    AFA I'm concerned.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 7, 2006
    #3
  4. John Goche

    Noah Roberts Guest

    Victor Bazarov wrote:
    > John Goche wrote:
    > > I would like to know whethere there is a difference between
    > > a const variable and a static const variable inside a class.
    > > After all, if a variable is const in a class, the compiler can
    > > always optimize and turn it into a static const variable to
    > > save runtim memory/stack space since being const it
    > > cannot be changed. (?)

    >
    > I am not sure under what circumstances the non-static const data
    > member can be optimized away. If the object is non-const to begin
    > with, something can always cast away the const-ness and change the
    > data member without much of a concern, really.


    If the data member is const then casting the object itself to/from
    const has no affect on the undefined nature of changing that member.
    So I don't think it needs to be a different object per instance for
    that reason.

    However, since you can initialize const data members with data passed
    into the constructor those values can be different per instance to
    begin with and can be based on runtime calculated values. For that
    reason most of the time the data member needs to be one per instance
    and can't be optimized away. I doubt any compiler author would spend
    much time looking for the few times when it could be.

    A code sample to illustrate for the OP:

    struct X
    {
    const int x;
    X() : x(0) {}
    X(int y) : x(y) {}
    };

    int main(void)
    {
    X x1;
    X x2(5);
    X x3(9);

    std::cout << x1.x << "\n";
    std::cout << x2.x << "\n";
    std::cout << x3.x << "\n";

    int y;
    std::cin >> y;

    X x4(y);

    std::cout << x4.x << "\n";
    }
    Noah Roberts, Dec 7, 2006
    #4
  5. John Goche

    Evan Guest

    Victor Bazarov wrote:
    > John Goche wrote:
    > > I would like to know whethere there is a difference between
    > > a const variable and a static const variable inside a class.
    > > After all, if a variable is const in a class, the compiler can
    > > always optimize and turn it into a static const variable to
    > > save runtim memory/stack space since being const it
    > > cannot be changed. (?)

    >
    > I am not sure under what circumstances the non-static const data
    > member can be optimized away. If the object is non-const to begin
    > with, something can always cast away the const-ness and change the
    > data member without much of a concern, really.


    My impression was that if you did this to an object that was declared
    const the result was undefined behavior. Am I mistaken on this point?

    My understanding was if you have a non-const object that gets turned
    into a const object at some point, you can cast away the constness, but
    if the object was declared const then you can't be guaranteed that it
    will work as you wish.

    (Though in the context of this discussion, as other posters have said
    there is a difference between const and static const because a const
    member just has to be const within any given object, but different
    objects may have different values for that variable.)

    Evan
    Evan, Dec 8, 2006
    #5
  6. Evan wrote:
    > Victor Bazarov wrote:
    >> John Goche wrote:
    >>> I would like to know whethere there is a difference between
    >>> a const variable and a static const variable inside a class.
    >>> After all, if a variable is const in a class, the compiler can
    >>> always optimize and turn it into a static const variable to
    >>> save runtim memory/stack space since being const it
    >>> cannot be changed. (?)

    >>
    >> I am not sure under what circumstances the non-static const data
    >> member can be optimized away. If the object is non-const to begin
    >> with, something can always cast away the const-ness and change the
    >> data member without much of a concern, really.

    >
    > My impression was that if you did this to an object that was declared
    > const the result was undefined behavior. Am I mistaken on this point?


    No, but if you do it in a member function that is not declared 'const',
    what could be wrong? Don't do it in a c-tor or d-tor, of course.

    > My understanding was if you have a non-const object that gets turned
    > into a const object at some point, you can cast away the constness,
    > but if the object was declared const then you can't be guaranteed
    > that it will work as you wish.


    Any data member of a non-const object is non-const to begin with (the
    memory is allocated in a mutable area of the program full storage).
    The 'const' qualifier for a member is a precaution, not a lock.

    > (Though in the context of this discussion, as other posters have said
    > there is a difference between const and static const because a const
    > member just has to be const within any given object, but different
    > objects may have different values for that variable.)


    Yes. You initialise any non-static data member in the constructor
    initialiser list. Since it is possible to give different values to
    const non-static members, that's probably why they are non-static.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 8, 2006
    #6
    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. Rakesh Sinha
    Replies:
    4
    Views:
    1,846
    Rakesh Sinha
    Jan 13, 2005
  2. Dave
    Replies:
    10
    Views:
    35,262
    Ron Natalie
    May 22, 2005
  3. Javier
    Replies:
    2
    Views:
    557
    James Kanze
    Sep 4, 2007
  4. Thinking_In_CPP
    Replies:
    3
    Views:
    475
    James Kanze
    Feb 13, 2008
  5. er
    Replies:
    3
    Views:
    382
Loading...

Share This Page