0

//In Globals.hpp

const int SINE_MIN = 0;    // only CONSTANT
const int SINE_MAX = 1;    // only CONSTANT

static const int COS_MIN = 1;   // STATIC CONSTANT
static const int COS_MAX = 0;   // STATIC CONSTANT

What are the difference between Static Const & Const Variable ?

And if Same Variables are declared in a Class.

class SomeClass 
{
    const int SINE_MIN = 0;    // only CONSTANT
    const int SINE_MAX = 1;    // only CONSTANT

    static const int COS_MIN = 1;   // STATIC CONSTANT
    static const int COS_MAX = 0;   // STATIC CONSTAN 
}

I want to Set value at runtime as COS_MAX = getCosMaxFromFile() and after that it should not be changed so Made it Const .

DesignIsLife
  • 510
  • 5
  • 11

1 Answers1

2

First of all, if you declare something as const it means you can not change it. So no you can't assign to it in runtime. You can however call your function in the initialization:

static const int COS_MAX = getCosMaxFromFile();

Now then to the difference between static and non-static variables, it depends on where the variable is defined: If it's defined in file-global scope, or function-local scope. If it's in file-global scope then static variables are available only in the current translation unit (i.e. only in that source file). If it's a function-local variable, then a static variable will not loose its value between calls like other (non-static) local variables.

When you have a global static variable defined in a header file, that means that every source-file which includes the header file will have that variable defined. It won't cause linker errors though, because the variable is not visible outside each translation unit.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • There is no need for 2nd of all in the post -> foreach(argument in arguments) if the arguments is a list of 1 item there will never be the second or third of all. (case closed). – cerkiewny Apr 15 '14 at 12:16
  • But variables ≠ constants, especially when it comes to linkage (`const` implies `static`). – Konrad Rudolph Apr 15 '14 at 12:17