0

I am having trouble understanding why there are some options in c++ in order to avoid changes in variables value if an exception is always thrown which alerts the user that you are redefining a variable. I am referring for instance, to defining a variables as:

const int x=55 ;

In my short experience with C++, I can't tell why writing const is useful at all. Also related to this, I am guessing that when you work with public clases for instance, redefining a variable would get the same alert by C++. Why are then useful private clases if data members cannot be changed?

Do these unexpected changes in values of variables occur under some special conditions which are much less common?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Maths64
  • 127
  • 5
  • 1
    const means that variable should not be possible to change using any means. You should clarify what kind of unexpected changes you observe – mvp Jun 22 '20 at 10:24
  • 3
    By "redeclaring" you mean another variable with the same name, right? That's never possible, as you noticed, but `const` protects you against *reassigning*, e.g. `const int x=55; x=13;` would be an error. – Yksisarvinen Jun 22 '20 at 10:24
  • There are many threads in the site about these matters like https://stackoverflow.com/a/4064315/6865932 or https://stackoverflow.com/questions/2812875/how-const-keyword-works-in-c, I'm sure there are many more, I believe you can find all the information you need by researching this. – anastaciu Jun 22 '20 at 10:27
  • 1
    Imagine you have a named variable named `pi` that is iniitalised to a value with sufficient precision (for your program) to represent the math constant. Imagine that variable is used to do things such as calculating the area of a circle or radius of a circle given its radius. Now, imagine that some other code is allowed to access that variable, and change its value to `42`. Would it be better to disallow that? If you would prefer that calculations of areas or circumferences of circles are always correct (at least, within limits of numerical precision) then `const` is very useful. – Peter Jun 22 '20 at 10:35
  • @Yksisarvinen. Yes, I mean exactly that. Thanks all for your your help! – Maths64 Jun 22 '20 at 10:36
  • 2
    "...to avoid changes in variables value if an exception is always thrown which alerts the user that you are redefining a variable..." -- An exception being thrown is a run-time occurrence. A variable redefinition is a compile-time occurrence. Neither has something to do with attempting to write to a `const`, which causes a (different) compiler error -- or undefined behavior, if you did cast away the `const`. – DevSolar Jun 22 '20 at 10:37
  • It stops you from doing stupid stuff by mistake. Imagine you write `int SECONDS_PER_MINUTE = 60;` and then some day you discover that it's 61, what do you do? But if you write `const int SECONDS_PER_MINUTE = 60;` then you instantly know that stupid stuff is not allowed. – user253751 Jun 22 '20 at 11:22
  • @user253751: ...like allowing for leap seconds... :-D – DevSolar Jun 22 '20 at 12:06

1 Answers1

3

The value of a const int will not change through the lifetime of the object in a well defined program. Neither expectedly nor unexpectedly.

If the behaviour of the program is undefined, then there can be any behaviour including what you expect, and what you don't expect. Undefined behaviour is to be avoided.

eerorika
  • 232,697
  • 12
  • 197
  • 326