To define constants in a c ++ class, you can use either of the following methods:
1. Define enumeration constants in the class definition body;
For example:
Class
{
Public:
Enum
{
Thread _ num = 100,
MEM_BLOCK_SIZE = 1024,
PORT = 8080
};
};
The defined constant values cannot be modified during the program running, and the values of these constants are determined during compilation;
2. Declare the use of static const to modify member constants in the class definition body, and initialize these Members in the class definition body; that is, define static member constants of the const type in the class definition body;
For example:
Class
{
Public:
Static const int THREAD_NUM;
Static const int MEM_BLOCK_SIZE;
Static const int PORT;
};
Const int A: THREAD_NUM = 100;
Const int A: MEM_BLOCK_SIZE = 1024;
Const int A: PORT = 8080; www.2cto.com
Constants defined in the class in these two methods cannot be modified during the program running; these two methods are also the basis for template specialization and skewness;
Author: testcs_dn