是const(c ++)可选吗? [英] is const (c++) optional?

查看:95
本文介绍了是const(c ++)可选吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据一些教程我读了一段时间,const声明使一个变量常量,即它不能以后更改。

但是我发现这个const声明abit不方便,因为编译器有时会给出错误像

不能将const int转换为int

或类似的东西。

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.

问题:
假设我不小心更改源代码中的变量我可以很高兴地忘记这个const的东西?

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

提前感谢

推荐答案

为什么你想放弃这样有用的功能,只是因为你犯了一个错误有时?更好地尝试和学习避免与 const 的错误,你从中获得的巨大帮助,从而确保您的代码正确。

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.

这篇关于是const(c ++)可选吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆