what static const variable do

I Know difference between static variable and const variable
static variable: we can't reinitialize the variable but we can update
const : we can't update the variable

i don't know what static const variable do
can u any one post answer
static const variables cannot be changed. You would normally use them to store hard limits
like the maximum length of a file name etc.
ajgamester, it depends on how/where you are declaring a variable static.

At file scope, for example, it means that the symbol will not be exported beyond the file in which it is declared. Within a class or struct it means that the symbol is accessible without a "this" pointer. It has nothing to do with reinitialization.
Just to add to jsmiths comments

A static, class scope object is accessible without a 'this' pointer because, no matter how many instances of the class you create within a running program, there will only ever be one instance or copy of the static member, be it a method or variable. This single static object is shared across all the instances.

A static, method scope variable is as ajgmester describes, it only gets initialised once.

The static variable at file scope is very rarely done in C++, but is quite common in C and is as jsmith described.

Applying the const operator to any of these variables makes the variable effectively read only. Applying it to a class method prevents the method from modifying any class member variables.

I think I've covered everything, I'm sure someone will point out any omissions or errors :)

Last edited on
Topic archived. No new replies allowed.