menu

What is the difference between a static and const variable

Static VS. Constant

A static variable means that the object’s lifetime is the entire execution of the program and it’s value is initialized only once before the program startup. All statics are initialized if you do not explicitly set a value to them.The manner and timing of static initialization is unspecified.

A const is a promise that you will not try to modify the value once set.

const is equivalent to #define but only for value statements(e.g. #define myvalue 2). The value declared replaces the name of the variable before compilation.

Reference:

Static (C++) - on MSDN

static vs. const - on StackOverflow



KF

Comments