The Benefits of Constants

前端 未结 14 1874
南笙
南笙 2021-02-04 07:46

I understand one of the big deals about constants is that you don\'t have to go through and update code where that constant is used all over the place. Thats great, but let\'s s

14条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 08:07

    In addition to whatever has been already said, declaring the variable as a constant gives more freedom for the optimizer. It can eliminate reading its values, it can eliminate need to create a temporary copy, it can eliminate the variable itself (this is especially true for the numeric constants).

    Another big use case for the constants is constant objects. Having a constant object (or giving away a reference to a constant object in a function) you can be sure your object is not modified, and only methods (called const methods) of that object are allowed to be called. This is however true for C++, I am not sure if the same concept is valid in Ada as well.

提交回复
热议问题