const constants and macro definitions difference

1, reproduced address:

http://blog.sina.com.cn/s/blog_60d6fadc01012i52.html

(1) Different compilers treatment

  define macros are expanded in the preprocessing stage.

  const constants are compiled using the operational phase.

(2) different types and security checks

  define the type of macro is not, do not do any type checking, just start.

  const constants have specific types, will perform at compile time type checking.

(3) different storage

  define macros just launched, how many places, how many times had begun, does not allocate memory.

  const constant assigned (heap may be a stack) in memory.

 

(4) const can save space and avoid unnecessary memory allocation. For example:  
        #define macro constant the PI 3.14159 //  
        const doulbe Pi = 3.14159; // not the case Pi into the ROM ......  
        Double Pi = I; Pi // allocate memory in this case, since no redistribution!  
        double I = PI; // compile macro replacement, memory allocation  
        double j = Pi; // no memory allocation  
        double J = PI; // macro substitution then, again allocate memory!  
        const constants defined from the assembly point of view, gives only a corresponding memory address, is not as immediate as #define given, therefore, the constant const defined only one copy of the program is running, and # define constants defined with a plurality of copies in memory. 
(5) improve efficiency. Normal compilers are not generally constant const allocated storage space, but they are stored in the symbol table, which makes it a constant during compilation, storage and no memory read operation, such that its efficiency is also high.

 

const and  #define comparison

    C ++ language can be used to define constants const, #define can also be used to define constants. But the former than the latter has more advantages:

(1) const constant data types, and data type without macro constants. The compiler can perform type-safety checks on the former. While the latter only character replacement, no type of security check, and may cause unexpected errors (marginal effect) in character substitution.

(2) Some integrated debugger can debug the const constant, but can not debug the macro constants.

 

l   [Rule 5-2-1 ] in a C ++ program only without using a macro constant const constant, i.e. constant const completely replace macro constants.

5.3  constants defined rules

l   [Rule 5-3-1 ] need constant publicly in header files, no publicly defined constants in header files. For ease of management, the constant can be centrally stored in different modules of a common header file.

l   [Rule 5-3-2 ] If a constant value is closely related to other constants, the relationship should be included in the definition, and are not to give some isolation value.

E.g:

const  float   RADIUS = 100;

const  float   DIAMETER = RADIUS * 2;

5.4  class constants

Sometimes we want some constants valid only in class. Because of constant #define macro definitions are global and can not achieve the goal, so take it for granted that should be implemented by modifying const data member. const data member does exist, but its meaning is not what we expect. const data members only in the lifetime of an object is constant, and for the entire class terms is variable, because the class can create multiple objects, the value of different objects that const data members can be different.

    Const can not be initialized in the class declaration data members . The following usage is wrong, because the object is not to create a class, the compiler does not know what the value of SIZE Yes.

    class A

    {…

        const int SIZE = 100; // error, in an attempt to initialize const data members of the class declaration

        int array [SIZE]; // error, unknown SIZE

    };

 

const initialization data member can only be initialized in the table class constructor, e.g.

    class A

    {…

        A (int size); // Constructor

        const int SIZE ;  

    };

    A :: A (int size): SIZE (size) // constructor initializes table

    {

      …

    }

    A a (100); // a target value of the SIZE 100

    A b (200); // object b 200 is the SIZE

 

    How can we build an entire class constants are constant it? Do not expect const data members, and should be implemented by the enumeration constants class. E.g

    class A

    {…

        enum {SIZE1 = 100, SIZE2 =  200}; // Enumeration Constants

        int array1[SIZE1];

        int array2[SIZE2];

    };

    Enumeration constants do not take up storage space object, they are all evaluated at compile time. Enum constant disadvantage: it is an implicit data type integer, the maximum value is limited, and can not represent floating-point (e.g., PI = 3.14159). sizeof (A) = 1200; wherein the enumeration does not occupy space.

                         enum EM {SIZE1 = 100, SIZE2  = 200}; // enum constant     sizeof (EM) = 4;


Guess you like

Origin blog.csdn.net/weixin_42187898/article/details/93626360