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

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

A constant is an identifier whose value remains constant during run time. The C language uses #define来定义常量 (called Macro constants). The C + + language is in addition to #define外还可以用const来定义常量 (called Const constants). 5.1 Why do I need constants
What's the trouble if you don't use constants to fill in numbers or strings directly in your program?

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

(2) Enter the same number or string in many parts of the program, and there is no writing error.

(3) If you want to modify the number or string, it will change in many places, both troublesome and error prone.

L "Rule 5-1-1" try to use the intuitive Changshilai to represent numbers or strings that will appear multiple times in the program.

For example:

#define MAX/* C Language Macro constant * *

const int MAX = 100; Const constants for C + + languages

const float PI = 3.14159; Const constants for C + + languages

5.2 Const and #define的比较
The C + + language can use const to define constants, or you can use #define来定义常量. But the former has more advantages than the latter:

(1) A const constant has a data type, while a macro constant has no data type. The compiler can perform type security checks on the former. For the latter, only character substitutions, no type security checks, and the substitution of characters may produce unexpected errors (marginal effects).

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

L "Rule 5-2-1" uses only const constants in C + + programs instead of macro constants, which are the const constants that completely replace macro constants.

5.3 Constant Definition Rule
L "rule 5-3-1" need to be exposed to a constant in the header file, do not need to expose the constant on the head of the definition file. For ease of management, the constants of different modules can be centrally stored in a common header file.

L "Rule 5-3-2" if a constant is closely related to other constants, the relationship should be included in the definition, and no orphaned values should be given.

For example:

const float RADIUS = 100;

const float diameter = RADIUS * 2;

Constants in class 5.4
Sometimes we want certain constants to be valid only in the class. Because the macro constants defined by #define are global and do not achieve the purpose, it is of course thought that you should use const to modify the data members to implement. The const data member does exist, but its meaning is not what we expect. The const data member is constant only for an object lifetime and is mutable for the entire class, because the class can create multiple objects, and the values of the const data members of different objects can be different.

You cannot initialize a const data member in a class declaration. The following usage is incorrect because the compiler does not know what the value of the size is when the object of the class is not created.

Class A

{...

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

int array[size]; Error, Unknown size

};

The initialization of the const data member can only be done in the initialization table of the class constructor, for example

Class A

{...

A (int size); Constructors

const int SIZE;

};

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

{

...

}

A A (100); The size value of object A is 100

A B (200); The size value of object B is 200

How do you build constants that are constant in the entire class? Do not expect the Const data member, you should use the enumeration constants in the class to implement. For example

Class A

{...

enum {SIZE1 = +, SIZE2 = 200}; Enumeration constants

int ARRAY1[SIZE1];

int array2[size2];

};

Enumeration constants do not occupy the storage space of objects, and they are evaluated at compile time. The disadvantage of an enumerated constant is that its implied data type is an integer, its maximum value is limited, and it cannot represent a floating-point number (such as pi=3.14159).

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.