What is the use of Const?

0

I wanted to know what const does? According to the tutorials, I realized that the line of code is immutable. Now my question is, where does it become immutable? In the program that opens? Or does it mean coding itself?
And look at the following code that I am writing ...
When I change the variables a, b, c to 4, 6 and 9 and below a, b, c, a line is drawn and the program cannot be compiled ...
I want to understand exactly what the use of this const is in, for example, a game or program ...

#include<iostream>
#include<fstream>

using namespace std;

int main()
{

const int a = ۲;
const int b = ۵;
const int c = ۳;

a = ۴;
b = ۶;
c = ۹;

const int sum = a + b + c;

cout << sum << endl;
5

Now my question is, where does it become immutable? In the program that opens? Or does it mean coding itself?

If I understand the question, the answer is: both.

  1. It's checked on the compiler level, so in your example the code won't even compile, because you can't change value of a const.
  2. You could try doing const_cast to get rid of const, and compiler would be happy. There are also some tricks like getting address of a variable and then changing the memory using this pointer. But in fact const might go into memory location in the binary which is set to read only. This means that trying to change it will crash the application at runtime.

I want to understand exactly what the use of this const is in, for example, a game or program ...

It means the value will never change :) This is a very useful property for example in concurrency/multithreading. If you know the value is never changing there is no issue in sharing it between many threads. If this is not the case, you might need to ensure some atomic access, so you don't accidentally read "part" of the value or some "inconsistent" value, when other thread is changing the variable value (notice that not every variable assignment might be atomic!)

2

I want to understand exactly what the use of this const is in, for example, a game or program

It's hard to explain with trivial example why immutable state is so desirable, here you have example of how code looks like in practice https://github.com/qgis/QGIS/blob/9ee235cfbd79e02a719e3a49c490b6b4fb9ae9e7/src/core/providers/gdal/qgsgdalprovider.cpp It's not example of pretty code, but it might be a good example of what kind of a code we have to work with every day. After many months of reading and trying to understand such modules you're starting to realize that, you need to focus much less when you know, that certain variable value won't change. Also, when you're writing code with const everything I can and do not expose variable to code which does not need it mindset, code will start to naturally structure itself as you will be forced to move certain portions of the logic to separate entities to achieve it.

0

Another** important** issue is compiler optimization. All **const **variables and objects enable compiler to produce in some cases remarkable simply code.
You can also read https://www.bfilipek.com/2016/12/please-declare-your-variables-as-const.html

1 użytkowników online, w tym zalogowanych: 0, gości: 1