Const (C ++) optional?

according to some tutorials that I read some time ago, declaring “const” makes the variable “constant”, i.e. it cannot be changed later.
But I find this const abit declaration inconvenient because the compiler sometimes gives errors, for example, "cannot convert const int to int"
or something like that.

and I cheat by deleting it anyway.

Question: Assuming that I do not cautiously change the variable in my source code , can I happily forget about this const material?

Thanks in advance

+4
source share
11 answers

Are you seriously? Why do you want to refuse such a useful function just because you sometimes make a mistake? Better try and learn how to avoid errors with const and you will get a lot of help that he adds to ensure your code is correct.

Of course, you can say goodbye to all the help provided by the language and tell the compiler, thereby no longer telling you about errors in your code. Instead, you will have to ask the debugger later where your errors are. Not sure if this is better.

+51
source

In C ++, "const" can be applied to a variable (making it immutable) or to a function (which makes it impossible to change other things).

My use of "const" is not only that my code does not change my variable. To prevent some idiot code from changing my variable (especially if the idiot is me after six months) and to prevent my code from changing the critical variable, some idiot remains open (especially if the idiot was six months old to me) back).

+14
source

If you are careful, yes. But man is mistaken. In addition, you do not give the compiler the opportunity to optimize around these constants.

The error message you receive is related to the fact that you are trying to change const int . Just assigning a value (not const) int , you change it the way you want.

Const helps you, try sprinkling it with plenty and listening to the compiler. This will help you improve the code.

+6
source

Even if you and everyone you work with never make mistakes, using const in your method declarations helps document your interface.

+3
source

You lose some useful features of the language without const, especially with respect to references. Consider:

 void f(const MyClass& m); void f(const int& i); // ... f(MyClass()); // OK, can pass temporary as const reference f(5); // OK as well, 5 is a temporary int 

If you consider "const" optional and get rid of it:

 void f(MyClass& m); void f(int& i); // ... f(MyClass()); // Error, cannot pass temporary as non-const reference f(5); // Error, same as above 
+3
source

The idea of ​​using 'const' is that you guarantee compiler errors when you try to change the value of a variable when it was predetermined (using const ), that you DO NOT want to do this. It is essentially built in error checking and is useful for many reasons.

This is especially valuable in cases such as the external interface and public methods, as a way to guarantee the caller that the passed value will not be changed.

const also blocks the intention of not changing and prevents accidental assignment.

When using const mandatory use is optional, it is very useful and useful.

Here is a useful explanation you can check: http://duramecho.com/ComputerInformation/WhyHowCppConst.html

+2
source

You can forget about it, but isn't it nice if the compiler forces you to do this? This way your const variables always remain constant.

+1
source

Its always optional. If all of its code is sure that you can forget it (I would not recommend it because it protects you), but when you interact with others, you essentially provide them with a contract that you will not change your object or call the function changes the state of your object. This can be invaluable if you are not familiar with another code or you do not have a source.

+1
source

First answer the question:

Yes, you can. But only if you are careful, and everyone else who uses your code from now on to eternity is also careful.

So, in balance, you better think about why you should do something const , and when not.

Another technique for exploring why const matters is to try to do everything const first, until you have good reason to change something, and only then remove the minimum number of constants until it works again.

Glad to see what you think about the problem - there is more than most.

+1
source

Usually people encounter this problem when they start using the const keyword. Believe me, it really helps. Leave this to the compiler to take care of the cosntness of the variable, instead of taking care not to change its value anywhere.

0
source

Changing things that should not be changed is one of the most common sources of error. Therefore, it is worth specifying const, because it prevents you from doing something wrong. Why would you do this?

 const double PI = 3.14159265358979; PI=4; // generates a compiler error (good) 

There are some problems with C ++ notation, because a constant can be initialized, but not assigned for the first time, and sometimes you do not matter during initialization.

 class A { private: const int num; public: A(int x, int y) : num(0) { // oops, I don't yet know what num should be while ( ... ) { } num = ...; } }; 

The way out of this is to define a private function that calculates the num value, but sometimes this means that instead of a single clean block of code in the constructor, you are forced to partition it uncomfortably, just so that you can initialize the variable.

 class A { private: const int num; int computeNum(int x, int y) { ... } public: A(int x, int y) : num(f(x,y)) { } }; 

Sometimes you have a value that should usually be const, but you want to selectively redefine it when it semantically makes sense. For example, social security numbers do not change unless your identity is stolen. Thus, you have only one method called createNewSSN (), which changes the constant constant ssn

 class Person { private: const int ssn; public: Person(int ssn_) : ssn(ssn_) {} void createNewSSN(int newssn) { log << "Changed SSN: " << ssn << " to " << newssn << "\n"; *(int*)&ssn = newssn; // trust me, this is a special case.... } }; 
-4
source

Source: https://habr.com/ru/post/1304568/


All Articles