(1) different compiler processing methods
Define macros are expanded during the preprocessing phase.
Const constants are used during the compile run phase.
(2) Different types and security checks
Define macros have no type, do not do any type checking, just expand.
Const constants have specific types that perform type checking during the compilation phase.
(3) different storage methods
The Define macro is only expanded, how many places are used, how many times it is expanded, and no memory is allocated.
Const constants are allocated in memory (either in the heap or in the stack).
(4) const can save space and avoid unnecessary memory allocation
Const definition constants from the assembly point of view, just give the corresponding memory address, instead of the immediate number as given in # define, so the const definition of the constant in the program run only one copy, and #define定义的常量在内存中有若干个拷贝.
For example:
#define PI 3.14159//Macro Constants
Const DOULBE pi=3.14159; Pi is not placed in ROM at this time ...
Double I=pi; Allocate memory for PI at this time, no longer assigned!
Double I=pi; Macro substitution during compilation, allocating memory
Double J=pi; No memory Allocations
Double J=pi; Another macro replacement, another memory allocation!
(5) Improved efficiency.
The compiler typically does not allocate storage space for ordinary const constants, but instead saves them in the symbol table, which makes it a constant during compilation, without the storage and read memory operations, making it highly efficient.
Const data members are constants only for the lifetime of an object and are mutable for the entire class, because a class can create multiple objects, and the values of its const data members can be different for different objects.
Const cannot be initialized in a class declaration data members .
The following usage is incorrect, because the compiler does not know what the value of size is when the object of the class is not created.
class a{ constint// error, attempting to initialize const data member int//< in class declaration /c8> error, unknown size};
The initialization of a const data member can only be done in the initialization table of the class constructor, for example:
classA {... A (intsize);//constructor FunctionConst intSIZE; }; A::a (intSize): size (size)//initialization Table of constructors{...} A A ( -);//object A has a size value ofA B ( $);//Object B has a size value of
How can you build constants that are constant throughout the class? Const data members (which can be used with a static variable) should be implemented with enumeration constants in the class. For example:
class A {... enum - $ // Enumeration Constants int Array1[size1]; int array2[size2]; };
Enumeration constants do not consume objects ' storage space, they are evaluated at compile time.
The disadvantage of an enumeration constant is that its implied data type is an integer, its maximum value is limited, and it cannot represent a floating-point number (such as pi=3.14159).
sizeof (A) = 1200, where enumeration does not occupy space.
Enum EM {SIZE1 = +, SIZE2 = 200}; Enumeration constant sizeof (EM) = 4;//Because the enumerator is treated as an integer, the result is all 4.
#define && Const