is const (c++) optional?

前端 未结 11 663
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-11 21:06

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 con

11条回答
  •  遥遥无期
    2020-12-11 21:14

    To answer your question first:

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

    So, on balance you are better off thinking about why you should make something const and when you should not.

    Another technique for exploring why const makes a difference is to try to make everything const at first until you have valid reasons to change something, then, and only then, remove the minimum number of consts until it works again.

    Glad to see you are thinking about the issue - its more than most do.

提交回复
热议问题