A brief analysis of the const keyword in c++

  • 2020-05-26 09:40:35
  • OfStack

const is a qualifier of the C++ language, which qualifies 1 variable and is not allowed to be changed. Using const improves the security and reliability of the program to a certain extent. In addition, a clear understanding of the role of const when looking at other people's code is helpful in understanding their programs.

The difference between define and const

1.define is used for simple character substitution during preprocessing

2. const has the function of type checking at compile time

3. const must be initialized

Constant Pointers and pointer constants


 #include <iostream>
using std::endl;
using std::cout;
int main()
{
  int a = 100;
  const int *pa = &a;
  int * const pb = &a; 
     return 0;
}

Summary:


const int *pa = &a;--> Pointer to a constant 

You can change the pointer, not the value of the referent variable.


int * const pb = &a;

You cannot change the pointer point, you can change the value of the referent variable.


Related articles: