Why is const-correctness specific to C++?

后端 未结 14 603
盖世英雄少女心
盖世英雄少女心 2020-12-23 16:42

Disclaimer: I am aware that there are two questions about the usefulness of const-correctness, however, none discussed how const-correctness is necessary in C++ as oppos

14条回答
  •  旧巷少年郎
    2020-12-23 17:05

    You're right, const-correctness isn't necessary. You can certainly write all your code without the const keyword and get things to work, just as you do in Java and Python.

    But if you do that, you'll no longer get the compiler's help in checking for const violations. Errors that the compiler would have told you about at compile-time will now be found only at run-time, if at all, and therefore will take you longer to diagnose and fix.

    Therefore, trying to subvert or avoid the const-correctness feature is just making things harder for yourself in the long run.

提交回复
热议问题