First, to talk about the const
In general,Const is primarily used to prevent defined objects from being modified again, and to initialize variables when defining object variables.
Common usage is as follows:
1. Used to define a constant variable so that the variable can no longer be modified at a later
const int Val = 10;
Val = 20; Error, cannot be modified
2. The parameter is not modified when the parameters are protected, if the value of this parameter is modified by using a reference to pass the parameter or pass the argument by address,
The value of the variable passed in by the function is also changed, and if you want to protect the passed variable from being modified, you can use the Const protection
void fun1 (const int &val)
{
val = 10; Error
}
void fun2 (int &val)
{
val = 10; No errors
}
void Main ()
{
int a = 2;
int b = 2;
Fun1 (a); Because of an error, the value of a at the end of the function is still 2.
Fun2 (b);//Because there is no error, the value of B at the end of the function is 10
}
If you only want to pass the value to the function, and this cannot be modified, you can use the Const protection variable, someone will ask why not pass by value, pass by value also need to copy this value again,
References are not required, and references are used to improve efficiency//If passed by value, there is no need to add a const, that doesn't make sense.
3. Save memory space,
#define PI 3.14//Use # define Macros
Const double PI = 3.14//Using const, when Pi is not put into memory
Double A = Pi; This is the time to allocate memory for PI, but there will be no memory allocated after this definition.
Double b = PI; allocating memory at compile time
Double C = Pi; No more memory is allocated,
Double d = PI; Allocate memory at compile time
A const-defined variable that allocates memory only once, and macro constants, defined by # define, can be allocated many times, so that the const is space-saving
4. Use the const modifier function in a class to prevent modification of non-static class member variables
Class
{
Public
void Fun () const//plus const modifier
{
A = 10; Error, non-static variable cannot be modified
b = 10; Yes, you can modify
}
Private
int A;
static int b;
}
5. Modifier pointers
const int *a; or int const *a; The const modifier points to the object, a variable, the object that a points to is not mutable
int *const A; Const modifier pointer A, a immutable, variable a pointing to an object
const int *const A; Pointers A and a object are not mutable
6. modifier function return value to prevent the return value from being changed
const int fun ();
Variables that receive return values must also be const
const int a = fun (); The variable being received is also const, and int a = Fun () is the wrong
7. Modifying member variables of a class
Variables that use the const modifier must be initialized, and cannot be initialized in the class at the time of definition.
such as
Class
{
Private
int a = 10;
const int b = 10;
static const int c = 10;
So the initialization is wrong,
}
Initializes a const int type (without static), which is initialized on the class's constructor.
Class Test
{
Public:
Test (): B (23)//The value of initialization B on the constructor is 23
{
}
Private
const int B;
}
Initializes the type of Staticconst int (with static), which is initialized outside the class.
Class Test
{
Private
static const int C;
}
const int test::c=10; The outer initialization of Class C is 10
A 8.const defined object variable can only be used by this program in the C + + file, and cannot be called by other C + + files of the program.
such as file1.cpp in const int val;
In the File2.cpp, the extern intval; Error, cannot be called,
A const-defined object variable can be called by another file, and must be defined using the extern modifier as
extern const int Val;
The non-const variable defaults to extern, and if Const can be accessed by another file, it must display the specified extern
Second, to talk about the typedef
(C++primer plus,p149,p248 and other related discussion)
It was simply assumed that Typdef's usage was only typedef a B, that is, type B as a new name for type A. But it is easier to misunderstand a definition like typedef string *pstring or typedef int INT_ARRAY[4].
Common difficulty usage is as follows:
1) Simplify the definition of pointers to multidimensional arrays with typedef
First, take one-dimensional arrays as an example:
For example typedef int INT_ARRAY[4], if the previous typedef is removed, then the definition is an array of 4 elements called Int_array. When preceded by a TypeDef, Int_array becomes the alternate name for the array type with 4 elements. In the future there will be Int_array IA, which we know is equivalent to int ia[4];
Examples of two-dimensional arrays can be consulted: trapsbetween typedef and const
2) Simplify the definition of function pointers with typedef
For example:
typedef BOOL (*CMPFCN) (conststring &, const string &);
The definition indicates that CMPFCN is the name of a pointer type that points to a function. The pointer type is a pointer to a function that returns a bool type with two const string reference parameters. When you want to use this function pointer type, you just use CMPFCN directly, and you don't have to write the entire type declaration all at once. For example: Define two new function pointers PF1 and PF2 with the newly obtained type,
CMPFCN PF1;
CMPFCN PF2;
Also assume that the following statement is given:
typedef string *pstring;
Const Pstring CStr;
Many people mistakenly consider it to be:
Const string *cstr;//const pstring is a pointer to a const object of type String
The reason for the error is that the TypeDef is expanded as text. When declaring a const pstring, the const modifier is the type of pstring, which is a pointer. Therefore, the declaration statement should be defined as a const pointer to a string object of type CStr, which is equivalent to: string *const CStr;
3) Simplify the definition of complex types with typedef
Limited space, more detailed usage interpretation recommended refer to:C + + typedef usage Summary (highly recommended)
or C + + typedef usage
A detailed description of the common usage of const and typedef