C89: on constant

Posted by nivaspm on Thu, 31 Oct 2019 20:19:11 +0100

I. Const constant

1. role

Const decorated data type refers to constant type. The value of variable or object of constant type cannot be updated.

It is used to improve the security and reliability of the program (in fact, I generally think it's not necessary to add it, but it's better to add the code).

Its original purpose is to replace precompiled instructions, because const defined constants have only one copy in the program running, and ා define defined constants have several copies in memory

Non Const types can be assigned to Const types, while Const types cannot be assigned to non Const types
Const decorated data type must be given initial value

 

2. Modify built-in type variable

//(1) basic types
int A;            //variable
const int a=1;    //constant
a=2;              //Error, constant immutable
 
int const b=2;    //constant
b=3;              //Error, constant immutable
 
 
 
//(2) the pointer determines whether const is combined on the left or right side of *
const int* p=&A;            //Constant data, variable pointer
int const* p=&A;            //Constant data, variable pointer
p=&a;                       //Correct, variable pointer can change
*p=a;                       //Error, constant data cannot be changed
 
 
int* const p=&A;            //Variable data, constant pointer
p=&a;                       //Error, constant pointer cannot change
*p=a;                       //Correct, variable data can change
 
 
const int* const a=&A;      //Constant data, constant pointer
a=&a;                       //error
*a=a;                       //error 

 

3. Modify function parameters

//pass by value
void A(const int a){     //Because it is two different blocks of memory, only the parameter variable memory in the lock function can not be modified.
    a=3;                 //Error, unable to modify
}  
 
//Citation transfer
void A(const int& a){    //Refers to the original data memory. The value of this memory cannot be changed.
    a=3;                 //Error, unable to modify
}  
 
 
//Pointer passing
void A(const int* a){    //The data pointed to cannot be changed, indicating that the pointer cannot modify the value pointed to memory.
    int b=1;
    a=&b;        //Correct, the pointer variable passed in can change
    *a=2;        //Error, the value of the pointer variable passed in cannot be changed
}  
 
//Note: the essence of pointer transfer is also value transfer. Any change to the pointer variable of a parameter will not affect the pointer variable of an actual parameter. Only when the memory pointed to by the pointer variable of a parameter is modified will the memory pointed to by the pointer variable of an actual parameter be affected.
void A(int* const a){
    int b=1;
    a=&b;        //Error, passed in pointer variable cannot be changed
    *a=2;        //Correct. The value of the pointer variable passed in cannot be changed.
}

 

4. Decorated return value

//pass by value
const int a(){}      //Meaningless, the return value plus the constant has no effect or change on the formal parameter and the actual parameter
 
//Citation transfer
const int& a(){}    //It's not recommended to write like this
 
 
//Pointer passing    
const int* A(const int* p){    //Use constant pointer as return value, and the pointer to receive must also be constant pointer
    return p;
}
 
int B(){
    int data=5;
    const int* p=&data;
    p=A(p);    //Return constant pointer
}
 
int* const A(){...}  //It's meaningless. It doesn't have any effect and change on formal parameters and actual parameters.
                     //Because after the pointer variable is returned, its change is meaningless. It is important to know whether the memory it points to is a constant.
 
 
//Note that const is not allowed for non member functions
int* A()const{...}    //Error, normal function cannot be qualified with const
 
//Note that const int * cannot be converted to int * and constant pointers cannot be converted to variable pointers
//However, int * can be converted to const int *, and variable pointers can be converted to constant pointers

 

5. Modify member function

Note: const decorated member function is only available in C + +.

int A() const{}    //Error, ordinary function cannot add upper limit specifier, only member function can be modified
 
 
class A{
    
public:
    
    //Qualify called member variables and member functions
    //const decorated member functions cannot modify any member variables (except mutable decorated variables)
    //Const decorated member functions cannot call non const member functions because non const member functions modify member variables
    void a()const;
    
 
};

Topics: PHP