The use of const in c++

  • 2020-04-01 23:33:57
  • OfStack

Const is a commonly used type modifier in C++. Constant type refers to the type specified by the type modifier Const. The value of a variable or object of constant type cannot be updated.

1. Define constants
(1)const modifies variables. The following two definitions are essentially the same. Const modifies the value of a variable of TYPE as immutable.

  TYPE const ValueName = value;
        Const TYPE ValueName = value;


(2) change the const to an external connection, act on global expansion, allocate memory at compile time, and can not be initialized, just as a declaration, the compiler thinks it is defined elsewhere in the program.

        Extend const int ValueName = value;

2. Use CONST for Pointers
(1) the pointer itself is constant and immutable
        (char *) const pContent;
        Const pContent (char *);

(2) the contents of the pointer are constant and immutable
        Const (char) * pContent;
        (char) const * pContent;

(3) neither can be changed
          Const char * const pContent;

(4) there are some ways to distinguish, draw a line along the * sign:
If const is to the left of *, then const is used to modify the variable that the pointer points to, that is, the pointer points to a constant;
If const is to the right of *, const is the modifier pointer itself, that is, the pointer itself is constant.

3. CONST is used in functions

(1) parameters of const modified function
A. The passed parameters cannot be changed in the function (nonsense, because Var itself is a parameter)

Void function (const int Var);

B. The parameter pointer refers to a constant and cannot be changed

Void function (const char * Var);

C. Parameter pointer itself is constant immutable (also meaningless, because char* Var is also a parameter)

Void function (const char * Var);

D. Parameters are referenced to increase efficiency and prevent modification. When modifying reference parameters:

Void function (const Class & Var); // reference parameters cannot be changed within a function

Void function (const TYPE & Var); The // reference parameter is constant in the function

2) return value of const modifier function
      The const modifier return value is not used very much, and it has the same meaning as const modifies ordinary variables and Pointers.
      A. const int fun1() // this is meaningless because the parameter return is itself an assignment.
      B. Const int * fun2() // const int *pValue = fun2();
                                                  // we can think of fun2() as a variable, that is, the contents of the pointer are immutable.
      C.i nt * const fun3 ()     // int * const pValue = fun2();
                                                  // we can think of fun2() as a variable, that is, the pointer itself is immutable.

4. Class related CONST

(1)const modifies member variables
Const modifies a member function of a class that represents a member constant, cannot be modified, and can only be assigned in the initialization list.
      Class A,
      {
              ...
              Const int nValue;                 // member constants cannot be modified
              ...
              A(int x): nValue(x) {}; // can only be assigned in the initialization list
        }

(2)const modifies member functions
Const modifies a member function of a class that cannot modify any non-const member functions in the class. It's usually written at the end of the function.
      Class A,
      {
             ...
            Void function () const; // constant member function, which does not change the member variable of the object.                                              

// also cannot call any non-const member function in the class.
}

(3)const modifies class object/object pointer/object reference

The & # 8226; Const modifies a class object to indicate that the object is a constant object and that no member of it can be modified. The same is true for object Pointers and object references.
The & # 8226; Const modifies an object in which any non-const member function cannot be called because any non-const member function has an attempt to modify a member variable.

Such as:


class AAA 
{  
    void func1();  
  void func2() const;  
}  
const AAA aObj;  
aObj.func1(); // x  
aObj.func2(); //correct

const AAA* aObj = new AAA();  
aObj-> func1(); // x  
aObj-> func2(); //correct

5. Method to convert Const type to non-const type

C++ provides four conversion operators:

The & # 8226; Const case < The new type > (expression)
The & # 8226; static_cast < new_type > (expression)
The & # 8226; reinterpret_cast < new_type > (expression)
The & # 8226; dynamic_cast < new_type > (expression)
 

Non-const casting using const_cast.  
Usage: const_cast < The type_id >   (expression)
This operator is used to modify the const or volatile property of a type. Type_id and expression are of the same type, except for const or volatile modifiers.

The & # 8226; The constant pointer is converted to an infinite pointer and still points to the original object.
The & # 8226; Constant references are converted to nonconstant references and still point to the original object;
The & # 8226; Constant objects are converted to nonconstant objects.


const int constant = 21; 
const int* const_p = &constant; 
int* modifier = const_cast<int*>(const_p); 
*modifier = 7; 

Of course, we can use the following traditional ways instead:

const int constant = 21; 
int* modifier = (int*)(&constant); 

As you can see from the previous code, we cannot change constant, but we can re-assign modifier.

But, but, is the programming world really messed up? Did we really modify constatn via modifier? Is the modification of const variable data really the purpose of C++ to const?

If we print out the results:


cout << "constant: "<< constant <<endl; 
cout << "const_p: "<< *const_p <<endl; 
cout << "modifier: "<< *modifier <<endl; 
 

Constant retains its original value.

But they do point to the same address:


cout << "constant: "<< &constant <<endl; 
cout << "const_p: "<< const_p <<endl; 
cout << "modifier: "<< modifier <<endl; 

 

While this can re-assign the value of const, never re-assign const data.


Related articles: