static const vs #define

前端 未结 11 1027
醉话见心
醉话见心 2020-11-22 13:01

Is it better to use static const vars than #define preprocessor? Or maybe it depends on the context?

What are advantages/disadvantages for

11条回答
  •  时光说笑
    2020-11-22 13:14

    • A static const is typed (it has a type) and can be checked by the compiler for validity, redefinition etc.
    • a #define can be redifined undefined whatever.

    Usually you should prefer static consts. It has no disadvantage. The prprocessor should mainly be used for conditional compilation (and sometimes for really dirty trics maybe).

提交回复
热议问题