2

I am doing research on static and const keywords.

Static: Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object.

const: You use the const keyword to declare a constant field or a constant local. This keyword specifies that the value of the field or the local variable is constant, which means that it can't be modified.

I would like to know about example when programmers would use static and const keywords.

1

3 Answers 3

3

const variables, or constant, would be used to declare a value that won't change or you want to prevent from being changed. For example, Pi could be declared as a constant in C++.

const double kPi = 3.14159265359; 

static variables are a bit different. There is only one instance of the static variable that persists across classes\functions.

For example:

void foo() { static int bar = 0; printf("%d\n", bar); ++bar; } int main() { int i; for(i = 0; i < 5; ++i) { foo(); } } 

Would print:

0 1 2 3 4 

Even though bar goes out of scope, its value is still in memory, so its only initialized once. Each time the foo() is called, that value gets incremented.

Edit:

To clarify, the compiler will actually reserve memory for the static variable within the assembly code that it produces. Additionally, the static keyword also tells the compiler to initialize the variable exactly once. The scope of the variable is the same (inside the foo function), but it is only initialized once in the above case. Automatic variables (such as int i) are pushed onto the call stack when the function is called.

Sign up to request clarification or add additional context in comments.

6 Comments

what if you try to overwrite on const variable>?
GCC complains throws an error because you are essentially saying that your const is not actually a const.
but everytime you are assigning int bar = 0; so why doesn't it get resset?
No you're not assigning every time you call foo(). When you use static you are telling the compiler to initialize the variable exactly once. If you look at the assembly produced by the compiler for the above example, you will find a space actually located in the assembly output to hold the static variable. Automatic variables (like int i in the above example) are pushed onto the function call's stack during runtime.
But your are specifying that the int bar=0. You are not only initializing but are also assigning a value. Just like bar++
|
2

(All of this is for Langage C++)

Hi, you can use const and static keywords in few cases, for exemple :

Const

First, used to say that the variable cannot be modified.

int main() { const int toto[4] = {0, 1, 2, 3}; return (0); } 

// We cannot modify toto after it declaration, why is it usefull? It keeps the state of your program design

Second, used to say that a method do not modify the object state.

class toto { int i; public: int geti() const; } 

// All getter in CPP use it. Why is it usefull? Developper who use the class know that he do not modify the object state

Third, used to say that the parameter pass to a function isn't modify by the function.

int function(const std::string &str) { // function cannot modify the object string pass in parameter } 

Static

First, used to say that a implemented function is know only inner a single file.

static int fct() { return (0); } 

// You can ony call function fct() if you are inner the file who it is implemented

Second, used to say that a argument or method is common to all object of the same class.

class toto { public : static int getLol(); }; 

// Every object totoObj1, totoObj2 ... will call same function

Third and the last one, used to say that a variable do not change state between multiple call of the same function where it is declared

void fct() { static i = 0; std::cout << ++i << std::endl; } 

// This function are going to print 1 then 2 then 3 ...

If you have any questions, you are welcome :)

Comments

0

If you want to have any variable/method to be available irrespective of an object you go for a static member but if you want the same to be unmodifiable as well then const is the solution. Following example can help you understand this.

In below example

  • PI is defined as a constant and can not/should not be changed
  • ONLINE holds number of users online, it can change but should be available irrespective of objects

    public class Example { //PI should not be changed, with reasons known that it is a constant public const double PI = 3.14; //Users currently using the application public static int ONLINE = 0; public Example(){ ONLINE++; } public void dispose(){ ONLINE--; } public static int loggedInUsers(){ return ONLINE; } public void GetArea(int radius){return PI*radius*radius; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.