The difference is as follows:
Constants defined with #define MAX 255 are of no type , given an immediate number, and the compiler simply links the defined constant value to the name of the defined constant, define the defined macro variable to be replaced at the time of preprocessing. Copy replacement is used where the constant is applied in the program;
with const float MAX = 255; The defined constants have type names, are stored in the static area of memory, the const variable has only one copy while the program is running, and the macro variable defined by #define has multiple copies. Therefore, the macro definition consumes more memory than the const variable in the process of running the program.
Constants defined with define can not be pointed to with pointer variables, constants defined with const are the addresses of the constants that can be pointed to by the pointer;
You can define simple functions with define, and const is not allowed to define functions.
Specifically, there are the following differences:
1. Compiler processing Mode
define– in the preprocessing phase
const– determines its value at compile time
2. Type check
define– no type, no type safety check, may produce unexpected errors
const– with data types, type checking at compile time
3. Memory space
define– does not allocate memory, gives the immediate number, how many times the use of the number of replacements, in memory there will be more than one copy, consuming large memory
const– allocates space in a static storage area, with only one copy in memory during program operation
4. Other
At compile time, the compiler usually does not allocate storage space for the const constants , but instead stores them in the symbol table , which makes it a constant during compilation , without the storage and reading memory operations, Makes it very efficient, too.
A macro substitution is a substitution, not a calculation, or an expression solution.
The scope of a macro definition is limited to the current file.
By default, the const object is valid only within a file, and when a const variable with the same name appears in multiple files, it is equivalent to defining separate variables in different files.
If you want to share a const object among multiple files, you must add the extern keyword (both declared and defined) before the variable definition.
Rules
Use only const constants in C + + programs without using macro constants, which are the const constants that completely replace macro constants .
http://blog.csdn.net/yingyujianmo/article/details/51206460