Constants in C ++

Source: Internet
Author: User
Tags constant definition
Original article: constants in c ++

A constant is an identifier whose value remains unchanged during running. 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 constant
If you do not use constantsProgramIs there any trouble to enter numbers or strings?

(1) the readability (comprehensibility) of the program is deteriorated. The programmer forgets what the numbers or strings mean,
Users do not know where they come from and what they represent.
(2) Inputting the same number or string in many places of 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.

Rules 5-1-1: Use constants with intuitive meanings to indicate the numbers or strings that will appear multiple times in the program.
For example:

 # Define  Max 100/* macro constant of C language */ 
Const Int Max = 100 ; // Const constant in C ++
Const Float Pi = 3.14159 ; // Const constant in C ++

5.2 const and # define comparison
the C ++ language can use const to define constants, or use # define to define constants. However, the former has more advantages than the latter:
(1) const constants have data types, while macro constants do not. The compiler can perform type security checks on the former. Only character replacement is performed for the latter, no type security check is performed, and character replacement
unexpected errors may occur (marginal effect ).
(2) some integrated debugging tools can debug const constants, but cannot debug macro constants.
[Rule 5-2-1] In the C ++ program, only the const constant is used instead of the macro constant, that is, the const constant completely replaces the macro constant.

5.3 constant definition rules
[Rule 5-3-1] constants that need to be made public are placed in the header file, and constants that do not need to be made public are placed in the header of the definition file. To facilitate management , constants of different modules can be stored in a common header file.
[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.
example:

  const     float   radius  =    100  ; 
const float diameter = radius * 2 ;

constants in the 5.4 class
sometimes we want some constants to be valid only in the class. Since the macro constants defined by # define are global and cannot be achieved, it is assumed that we should use const to modify data members. The const data member does exist, but its meaning is not what we expected.
the const data member is a constant only during the lifetime of an object, but it is variable for the entire class, because the class can create multiple objects, the values of the const data members of different objects can be different.
the const data member cannot be initialized in the class declaration. The following usage is incorrect because the compiler does not know what the size value is when the class object is not created.

  class   A 
{...
const int size = 100 ; // error, an attempt is made to initialize a const data member in the class declaration
int array [size]; /// error, unknown size
};

const data member initialization 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): size (size) /// constructor initialization table
{< br>...
}

A (100 ); // The size of object A is 100
a B (200); // the size of object B is 200
how can we create constants that are constant throughout the class? Don't count on the const data member. We should use the enumerated constants in the class. Example:

  class   A 
{...
Enum { size1 = 100 , size2 = 200 }; // enumeration constant
int array1 [size1];
int array2 [size2];
};

enumeration constants do not occupy the storage space of objects, they are fully evaluated during compilation. The disadvantage of an enumerated constant is that its implicit data type is an integer, its maximum value is limited, and it cannot represent a floating point number (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.