Yes, C++ is great. But, you don’t need to know everything to write great or good code, especially if we take into account the size of the STL. However, if you understand the basic rules in detail, all the other things that will take you to an “almost infinite” path will always be the same rules with intuitive specifications.
One of the basic things about C++ is the use of the const
keyword (constant, non-modifiable value). As we know, it is immutable/read only, that is, once a value is assigned, the declaration cannot be modified.
But, there are contexts of use. Let’s see “some sides” of C++ const!
If it declared, you need to initialize it immediately!
If you do this below:
By default, it does not compile. You will get an error:
■ Default initialization of an object of const type 'const int' (fix available)
.
If you are very stubborn, you can use the -fpermissive flag and you will be able to compile. I don’t know why this flag
exists, but I advise you never to use it.
If you want to compile it, you need to initialize it:
Any initial value must be defined when declaring the constant.
const
keyword positions are interchangeable for non-address values!
There is absolutely no difference between the statements below:
The only wrong way is to put
const
at the end of lvalue: ~int x const = 0;
~.
But, this rule only applies to not addresses, because when you are declaring ponteirs, the position has different results, that is:
1st. That:
2nd. It is other than:
In the first, you NOT can modify the ADDRESS that the pointer is associated with:
But, you can modify the VALUE that the pointer is pointing to:
x = &value2;
. And remember that addresses are different from values, so this:x = value2;
, without the (mis)reference sign (&
), independent ofconst
, is another type of error: ** Logic error!**
For the second case, it is the opposite of the explanations for the first!
You can still combine (use twice) the const
keyword in the same declaration, that is, make both the address and the value immutable:
With this, any other modification, whether of the value or the address, will not work.
Using the const
keyword at the end of the declaration of a MEMBER-FUNCTION (I highlighted, as this does not work in non-member functions), you just cannot modify any other member of a class. Example:
This doesn’t compile!
Unless you use const
at the beginning:
Or use another keyword: mutable
for the class member:
The keywords:
mutable
andconst
cannot be mixed, as a matter of logic!
Once a person enrolled in the Modern C++ Course asked me the “why” of being able to modify a variable that receives the returned value of a function (member or non-member) :
It seems a bit pointless, but the task of const
in this case only has to do with “no modifications” at runtime. Well, that really sounds strange and can be quite confusing, it would be another type of writing “style”!
However, the rules for parameters passed to functions: int myFunction(const int a)
(constant a
, in this case), are the same as if they were not a function parameter.
It may be “a lot” for some and “a little” for others. However, these 3 basic rules serve to contextualize which positions and places of use in C++ may or may not influence the final result.
If you want to go even deeper, I suggest the links below: