Usefulness of const (C++)

前端 未结 6 1165
眼角桃花
眼角桃花 2021-01-18 23:43

I\'m a const fiend, and I strive to make everything as const as possible.

I\'ve tried looking at various dissassembly outputs from const and non const versions of fu

6条回答
  •  死守一世寂寞
    2021-01-19 00:19

    This is not exactly a direct answer to the detailed version of your question, but it matches the title's question.

    I like to use const pretty aggressively too, in part because I think there's a minute change it will improve performance, but mostly because it reduces errors and communicates intent better.

    1. Having const methods allows users the freedom of having useful const objects.
    2. Having const objects allows library writers to succinctly communicate to users whether a function or method can change the input parameters.
    3. Having const objects allows you to have the compiler yell at you if you accidentally do something that will modify and object you don't want to modify.

提交回复
热议问题