And the difference between #define const of (turn)

The difference with a few from the point of view:

Angle 1:
definition constants say:
constant const variables are defined band type, #define constants defined only without types .

Angle 2:
In terms of stage work:
the DEFINE is active in the compilation of the preprocessing stage, and const is active when compiling, running.

Angle 3:
on the way in terms of function:
DEFINE simply replace the string, no type checking . Const and a corresponding data type is to be determined, the number of low-level errors can be avoided.
Because simply define string replacement can cause the boundary effect , the following specific examples may refer to the code:

#define N 2 + 3 // we expected value of N is 5, so that we use N
Double a = N / 2; the value of a is 2.5 // we expected, in fact the value of a is 3.5
. 1
2
angle 4 :
on the space occupied terms:
for example:

#define PI 3.14 / post / pre-occupied space snippet
const float PI = 3.14; // still essentially on a float, the space occupied by the data segment

angle of 5:
the terms of the ease of debugging code:
const constants debugging, define debugging is not, because in the pre-compilation stage has replaced the

Angle 6:
From the perspective of whether or not you can re-defined in terms of:
lack of const place, is born, const can not be redefined, and #define can cancel the definition of a symbol through #undef, and then redefined .

Angle 7:
In some special features in terms of:
DEFINE can be used to prevent duplicate header file references , and not const, can be found in the following codes:

// The following code in main header files, header files can be prevented from being repeated referenced
#ifndef // xxx xxx if not defined
#define defined // xxx xxx

// Here is your code

#endif // End If

PS: Below that, the drawbacks of the header file is repeatedly cited:
(1) some headers repeated references only increased the workload compilation work will not cause much of a problem, only the low efficiency of the compiler Some, but for large projects compile low efficiency it would be a very painful thing.
(2) Some header files contain repeat, it will cause an error, such as global variables defined in the header file (although this approach is not recommended, but it is allowed by the specification C) this will cause duplicate definitions.

Angles 8:
From the implementation point of view to achieve some complex functional point of view:
Use define the code that will look very simple, and can not realize the function const
e.g., six core achieved in the MFC mechanism, extensive use define
. 1, the MFC initialization procedure
2, the runtime type identification (RTTI)
. 3, dynamically create
4, permanent preservation
5, message map
6, messaging

For example, when implementing RTTI feature, we define the following macro code:

#define DECLARE_DYNCREATE(class_name) \

DECLARE_DYNAMIC(class_name)\

static CObject * PASCALCreateObject ();

and finally do something Summary:
from all angles above, we can see the difference between define and const at some point there is a certain similarity with the C language and JAVA language, that being said, the following reasons:
(. 1) the JAVA package internal height of the object, such as mechanisms for recovery of the memory is done automatically, the programmer does not need more ingenuity; C language, each application we use the pointer is a space, we requires careful consideration, the pointer will not have to use the back, now is not it can be released, will not become a field guide and so on, all of the memory-related issues need to be considered programmer, but the benefits are, C language can direct and convenient operation memory, which is a double-edged sword, with well more with less, used poorly, memory leaks, pointer running out, problems are everywhere, even caught a dump, is not good analysis with windbg (especially cross-module case)
differences in point (2) C language and JAVA language analogy to define and const, I just want to emphasize here define the role of very strong Large, although it is not the type of testing, not debugging, but also consider boundary effects, but because there is no type detection, pre-compilation is complete, which makes its use more flexible, more powerful, if we can make good use of define, often to play an unexpected effect.
----------------
Disclaimer: This article is the original article CSDN bloggers "aaronymhe", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/yi_ming_he/article/details/70405364

Guess you like

Origin www.cnblogs.com/Stephen-Qin/p/11514824.html