What is the difference between a macro and a const in C++?

前端 未结 7 1664
长情又很酷
长情又很酷 2020-12-01 02:08

I was asked this question in a technical interview:

What is the difference between a const and a macro in C++?

My a

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 02:49

    Another difference is that a const variable has a memory and can be referenced by a pointer. Macro is just the autocomplete that will happen before compilation, hence the name is lost during compiling.

    Also macro can be just more than a constant. It can be am expression or anything that is syntactically correct, even a whole definition of a function.

    Macros are used to depict programming choices e.g. stack size; while cosnt is used to depict the real world constants like value of Pi or e.

提交回复
热议问题