Difference between const and # define,

Source: Internet
Author: User
Tags constant definition

Difference between const and # define,

(1) Different compiler Processing Methods

The define macro is expanded in the preprocessing phase.
Const constants are used in the compilation and running phase.
(2) Different Types and security checks
The define macro has no type and does not check any type. It just expands.
Const constants have specific types. during compilation, the type check is performed.
(3) Different Storage Methods
The define macro is just an extension. If it is used in many places, it is expanded as many times without allocating memory.
Const constants are allocated in memory (either in heap or stack ).
(4) const can save space and avoid unnecessary memory allocation. For example:
# Define PI 3.14159 // constant macro
Const doulbe Pi = 3.14159; // at this time, the Pi is not put into the ROM ......
Double I = Pi; // at this time, the Pi is allocated with memory and will not be allocated later!
Double I = PI; // macro replacement during compilation and Memory Allocation
Double j = Pi; // no memory allocation
Double J = PI; // macro replacement, memory allocation again!
Const defines constants. From the assembly point of view, only the corresponding memory address is given, rather than the number of immediate values as # define. Therefore, the const-defined constants have only one copy during the program running, while the # define-defined constants have several copies in the memory.
(5) improved efficiency. Generally, the compiler does not allocate storage space for common const constants, but stores them in the symbol table, which makes it a constant during compilation without the operation of storage and read memory, this makes it highly efficient.


The C ++ language can use const to define constants, or use # define to define constants. However, the former has more advantages than the latter:

(1) const constants have data types, while macro constants do not. The compiler can perform type security checks on the former. However, only character replacement is performed for the latter, and there is no type security check. In addition, replacement may produce unexpected errors (marginal effect ).

(2) Some integrated debugging tools can debug const constants, but cannot debug macro constants.


Constant definition rules
In the C ++ program, only the const constant is used instead of the macro constant, that is, the const constant completely replaces the macro constant.
Constants that need to be made public are placed in the header file, and constants that do not need to be made public are placed in the header of the definition file. For ease of management, constants of different modules can be stored in a public header file.
If a constant is closely related to other constants, this relationship should be included in the definition, rather than some isolated values.
For example:
Const float RADIUS = 100;

Const float DIAMETER = RADIUS * 2;


Constants in the class
Sometimes we want some constants to be valid only in the class. Since the macro constants defined by # define are global and cannot be achieved, it is assumed that we should use const to modify data members. The const data member does exist, but its meaning is not what we expected. The const data member is a constant only within the lifetime of an object, but it is variable for the entire class, because the class can create multiple objects, the values of the const data members of different objects can be different.
The const data member cannot be initialized in the class declaration. The following usage is incorrect because the compiler does not know what the SIZE value is when the class object is not created.
Class
{...
Const int SIZE = 100; // error, attempted to initialize the const data member in the class declaration
Int array [SIZE]; // error, unknown SIZE
};
 
The const data member initialization can only be performed in the initialization table of the class constructor, for example
Class
{...
A (int size); // Constructor
Const int SIZE;
};
A: A (int size): SIZE (size) // initialization table of the constructor
{
...
}
A a (100); // the SIZE of object a is 100
A B (200); // the SIZE of object B is 200
 
How can we create constants that are constant throughout the class? Don't count on the const data member. We should use the enumerated constants in the class. For example
Class
{...
Enum {SIZE1 = 100, SIZE2 = 200}; // enumerated constant
Int array1 [SIZE1];
Int array2 [SIZE2];
};
Enumerated constants do not occupy the storage space of objects. They are fully evaluated during compilation. The disadvantage of an enumerated constant is that its implicit 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; enumeration does not occupy space.

Enum EM {SIZE1 = 100, SIZE2 = 200 };

// Enumeration constant sizeof (EM) = 4;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.