const v static const

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • themadme
    New Member
    • Mar 2007
    • 53

    const v static const

    Hello, i would kindly like to know what's the differences in between assigning a const to a variable or static const. Also which one is more sufficient.

    Especially with in a class, why not just use const, and initialise the members with in the constructor?

    i know const variable are compiled into hard coded values in the machine code and that static is stored on the .. stack or something. Anyway i know how it works in programmes.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by themadme
    i know const variable are compiled into hard coded values in the machine code and that static is stored on the .. stack or something. Anyway i know how it works in programmes.
    This is only true for global consts (which you shouldn't use) and static const class members.

    A const class member could have a different value for every instance of the class (initialised in the constructor) and therefore can not be compiled into hard coded values in the machine code.

    And that is the difference, static consts provide the same value for all instances of the class and are more efficient in terms of memory use and speed/size of code produced. consts allow for a different value for every instance of the class but are less efficient as they require memory and processing to access them.

    Comment

    • themadme
      New Member
      • Mar 2007
      • 53

      #3
      ahh yes, that would make sense. Thank you very much for your useful reply

      Comment

      Working...