(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. 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!
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定义的常量在内存中有若干个拷贝.
(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 and #define的比较
The C + + language can use const to define constants, or you can use #define来定义常量. But the former has more advantages than the latter:
(1) const constants have data types, and macro constants do not have data types. The compiler can perform type safety checks on the former. Instead of only character substitution, there is no type safety check, and the substitution of characters can produce unexpected errors (marginal effects).
(2) Some integrated debugging tools can debug const constants, but cannot debug macro constants.
L "rule 5-2-1" uses only const constants in C + + programs without using macro constants, that is, const constants completely replace macro constants.
5.3 Constant Definition Rules
L "rule 5-3-1" need to be exposed to the external constants in the header file, do not need to be exposed to the external constants in the definition file header. For ease of management, the constants of different modules can be centrally stored in a common header file.
L "rule 5-3-2" if a constant is closely related to other constants, the relationship should be included in the definition, rather than some orphaned values.
For example:
const float RADIUS = 100;
const FLOAT DIAMETER = RADIUS * 2;
Constants in the 5.4 class
Sometimes we want certain constants to be valid only in the class. Because the macro constants defined by # define are global and cannot be achieved, it is thought that the data members should be modified with Const. Const data members do exist, but their meaning is not what we expect. 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.
You cannot initialize a const data member in a class declaration . 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
{...
const int SIZE = 100; Error, attempting to initialize const data member in class declaration
int array[size]; Error, Unknown size
};
The initialization of a const data member can only be done in the initialization table of the class constructor, for example
Class A
{...
A (int size); constructor function
const int SIZE;
};
A::A (int size): size (size)//constructor initialization table
{
...
}
A (100); Object A has a size value of 100
A B (200); Object B has a size value of 200
How can you build constants that are constant throughout the class? Do not expect the Const data member to be implemented with enumeration constants in the class. For example
Class A
{...
enum {SIZE1 = +, SIZE2 = 200}; 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 the Minister space is enumerated.
Enum EM {SIZE1 = +, SIZE2 = 200}; Enumeration constant sizeof (EM) = 4;
Original link: http://blog.sina.com.cn/s/blog_79b01f6601018xdg.html