High Quality C ++C Programming Guide (Chapter 5 Constant)

zhaozj2021-02-17  57

Chapter 5 constant

Constants is an identifier that is constant during operation. C language uses #define to define constants (called a macro regular). C language can also be defined with constance (called Const) in addition to #define.

5.1 Why do you need a constant?

If you do not use constants, fill in the numbers or strings directly in the program, what will happen?

(1) Readability of the program (understandable) is deteriorated. The programmer will forget what the figures or strings mean, and the user doesn't know where they come, what is it.

(2) Enter the same number or string in many places in the program, and it is difficult to prevent writing errors.

(3) If you want to modify the number or string, it will be changed in many places, which is both trouble and easy.

l [Rules 5-1-1] Try to use the meaningful intuitive constant to indicate those numbers or strings that will appear multiple times in the program.

E.g:

#define max 100 / * C language Macroembox * /

Const int max = 100; // c language constant constant

Const float pi = 3.14159; // C language constant constant

5.2 Comparison of Const and #define

The C language can define constants with const, or use #define to define constants. But the former has more advantages over the latter:

(1) The constant constant has a data type, and the macro regular amount has no data type. The compiler can perform type security checking for the former. For the latter only characters, there is no type of security check, and it may generate unexpected errors (marginal effects) in characters.

(2) Some integrated debugging tools can debug a Const constant, but cannot debug a macro.

l [Rules 5-2-1] Only const constants are used in the C program without using a macro regular amount, that is, the constant constant is completely replaced.

5.3 Constant definition rules

l [Rules 5-3-1] Requires the constant publication of the foreign public in the header file, and does not require the heads of the definition files to the definition file. To facilitate management, you can store the constant concentration of different modules in a common header file.

l [Rule 5-3-2] If a constant is closely related to other constants, this relationship should be included in the definition, and some isolated values ​​should not be given.

E.g:

Const float radius = 100;

Const float diameter = radius * 2;

Constant in the 5.4 class

Sometimes we hope that some constants are only valid in the class. Because the macroemulus defined by #define is global, it cannot achieve the goal, so it is to certainly feel that it should be implemented with a const modified data member. The Const data member does exist, but its meaning is not what we expect. Const data members are constants only within an object survival, but for the entire class, it is variable because the class can create multiple objects, and different objects can be different.

CONST data members cannot be initialized in class declarations. The following usage is wrong, because the compiler does not know what the value of size is not created.

Class A

{...

const Int size = 100; // Error, attempted to initialize Const data members in class declarations

INT Array [size]; // error, unknown SIZE

}

The initialization of Const data members can only be performed in the initialization table of the class constructor, for example

Class A

{...

A (int size); // constructor

CONST INT Size;

}

A :: A (int size): initialization table of size (size) // constructor

{

...

}

A a a (100); // // The size value of the object A is 100

A b (200); // The Size value of the object B is 200

How can I build constant constant in the entire class? Don't expect const data members, you should implement the enumeration constants in the class. E.g

Class A

{...

ENUM {size1 = 100, size2 = 200}; // enumeration

Int Array1 [Size1];

Int array2 [size2];

}

Enumerations often do not take up the storage space of the object, and they are fully evaluated when compiling. The disadvantage of the enumeration is that its implied data type is an integer, its maximum value is limited, and cannot represent floating point numbers (such as PI = 3.14159).

转载请注明原文地址:https://www.9cbs.com/read-28725.html

New Post(0)