Thursday, August 5, 2010

High-quality C + + / C Programming Guide - Chapter 5 constant


Chapter 5 constant
Constant is an identifier, its value is constant during operation. C language defined using # define constants (called macro constant). C + + language in addition to # define const outside can also be used to define the constants (known as constants const).

5.1 Why do we need constant
If you do not use constants directly in the program fill in numeric or string, there will be any trouble?

(1) readability (intelligibility) worse. The programmer to forget what the numbers mean, or string, the user is more I do not know where they come from, that what.

(2) in many parts of the program enter the same number or a string, be sure to write the error does not occur.

(3) If you want to modify the number or string will be changed in many places, cumbersome and prone to error.


Rule 5-1-1銆?銆恗ake full use of the constant for the intuitive meaning of those who will appear many times in the program number or string.

For example:

# DefineMAX100 / * C language macro constants * /

constintMAX = 100; / / C + + language, the constant const

constfloatPI = 3.14159; / / C + + language, the constant const

5.2const comparison with the # define
C + + language you can use const to define the constants can also be defined using # define constants. But the former than the latter have more advantages:

(1) const constant with data types, but there is no macro data type constant. Compiler can be carried out on the former type of security check. While the latter only for character replacement, there is no type of security check, and replace characters may produce unexpected errors (marginal effects).

(2) Some integrated debugging tools for debugging on the const constant, but can not debug the macro constants.


5-2-1銆?銆恟ules in C + + program to use only const constant constants instead of macros, or completely replace the macro const constant constants.

5.3 Constants defined rules
5-3-1銆?銆恟ules open to the public's constant need to put a header file, do not open to the public of the constants in the definition file in the head. For management purposes, different modules can be stored in a constant concentration of public header file.

Rule 5-3-2銆?銆怚f a constant with other constants are closely related, should be included in the definition of this relationship, and not give some isolated values.

For example:

constfloatRADIUS = 100;

constfloatDIAMETER = RADIUS * 2;

5.4 class constants
Sometimes we hope that some of the constants is only valid in the class. Because # define macros defined constants are global and can not achieve their goals, so take it for granted that modify const data members should be achieved. const data member is indeed exists, but its meaning is not what we expect. const data members only during the lifetime of an object is constant, but in terms of the whole class is variable, because the class can create multiple objects, different object const data member whose value can be different.

In the class declaration can not initialize const data members. The following usage is wrong because the class object is not created, the compiler does not know what the value of SIZE.

classA

(...

constintSIZE = 100; / / error, attempt to initialize the const declaration in the class data members

intarray [SIZE]; / / error, unknown SIZE

);



const data member initialization can only be in the class constructor initializes the table, for example

classA

(...

A (intsize); / / Constructor

constintSIZE;

);

A:: A (intsize): SIZE (size) / / constructor initialization list

(

...

)

Aa (100); / / object a, SIZE value of 100

Ab (200); / / object b of the SIZE value of 200



How can we build in the entire class are a constant constant it? Do not expect const data members, and should use class enumeration constants to achieve. Such as

classA

(...

enum (SIZE1 = 100, SIZE2 = 200); / / enumeration constant

intarray1 [SIZE1];

intarray2 [SIZE2];

);

Enumeration constants will not take up storage space object, they are all evaluated at compile time. Enumeration constant drawback is: it's implicit data type is an integer, its maximum value is limited, and can not be said that floating-point (such as PI = 3.14159).






相关链接:



Neat old photographs - photo repair With Image Doctor



download converter Mp4 to 3gp



"Tencent Night Awards" announced this year's Great Wall Advertising Festival award the fifteenth



Contact HOME Linkist then the new tactics: to provide professional exhibition space



convert vob to flv



Dan Zhongshao: I want to lead by two years



windows 7 rmvb



My favorite Food And Drink



Production of fine wood decorative painting



Astrology Or Biorhythms Or Mystic Expert



WHAT dealers need to support?



FTP Site Preferences



Cable CONSTRUCTION should focus on good single-disc test



ts file EXTENSION



No comments:

Post a Comment