const (c++) 是可选的吗?

2022-01-23 00:00:00 constants c++

according to some tutorials i read a while back, the "const" declaration makes a variable "constant" ie it cannot change later.
But i find this const declaration abit inconveniencing since the compiler sometimes gives errors like
"cannot convert const int to int"
or something like that.

and i find myself cheating by removing it anyway.

question: assuming that i am careful about not changing a variable in my source code, can i happily forget about this const stuff?

Thanks in advance

解决方案

Are you serious? Why would you want to give up on such a useful feature just because you make a mistake sometimes? Better try and learn to avoid mistakes with const and you benefit from the great assistance it adds to ensure correctnes with your code.

Of course, you can say goodbye to all the help the language provides, and tell the compiler thereby not tell you about mistakes in your code anymore. Instead, you will have to ask the debugger later on where your bugs are. Not sure whether that's better.

相关文章