High-quality C++/C Programming Guide-5th-Constants

Source: Internet
Author: User
Tags definition constant constant definition constructor header int size

A constant is an identifier whose value is constant during operation. The C language uses #define to define constants (called macro constants). In addition to #define, the C language can also use const to define constants (called const constants). 5.1 Why do you need constants
If you don't use constants, what trouble will you fill in numbers or strings directly in the program?

(1) The readability (intelligibility) of the program becomes poor. Programmers themselves forget what those numbers or strings mean, and users are even more unaware of where they come from or what they represent.

(2) Entering the same number or character string in many places in the program makes it difficult to avoid writing errors.

(3) If you want to modify a number or string, it will be changed in many places, which is both troublesome and error-prone.

l [Rules 5-1-1] Try to use constants with intuitive meaning to represent the numbers or strings that will appear multiple times in the program.

E.g:

#define MAX 100 / * C language macro constant * /

const int MAX = 100; // C language const constant

const float PI = 3.14159; // const constant in C language

5.2 Comparison of const and #define
C language can use const to define constants, or #define to define constants. But the former has more advantages than the latter:

(1) const constants have data types, while macro constants have no data types. The compiler can perform type safety checks on the former. The latter only performs character replacement, there is no type safety check, and unexpected character errors (marginal effects) may occur during character replacement.

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

l [Rule 5-2-1] In C programs, only const constants are used instead of macro constants, that is, const constants completely replace macro constants.

5.3 Constant definition rules
l [Rule 5-3-1] Constants that need to be exposed to the public are placed in the header file, and constants that do not need to be exposed are placed at the head of the definition file. For easy management, you can store the constants of different modules in a common header file.

l [Rule 5-3-2] If a certain 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;

5.4 Constants in the class
Sometimes we want certain constants to be valid only in the class. Since the macro constants defined by #define are global and cannot achieve their purpose, they take it for granted that const should be used to modify data members. const data members do exist, but their meaning is not what we expected. The const data member is constant only during the lifetime of an object, but it is variable for the entire class, because the class can create multiple objects, and the value of the const data member can be different for different objects.

You cannot initialize const data members in class declarations. The following usage is wrong, because when the object of the class is not created, the compiler does not know what the value of SIZE is.

class A

{...

const int SIZE = 100; // Error, attempt to initialize const data member in class declaration

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

};

const data members can only be initialized in the initialization table of the class constructor, for example

class A

{...

A (int size); // Constructor

const int SIZE;

};

A :: A (int size): SIZE (size) // Initialization table of the constructor

{



}

A a (100); // SIZE value of object a is 100

A b (200); // SIZE value of object b is 200

How can we build constants that are constant throughout the class? Don't expect const data members, they should be implemented with enum constants in the class. E.g

class A

{...

enum {SIZE1 = 100, SIZE2 = 200}; // enum constant

int array1 [SIZE1];

int array2 [SIZE2];

};

Enumerated constants do not occupy the storage space of objects, they are all evaluated at compile time. The disadvantage of an enumeration constant is that its implicit data type is an integer, its maximum value is limited, and it cannot represent floating-point numbers (such as PI = 3.14159).

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.