C ++ usage of const



1、

const modifies ordinary variables


const modifies a variable, meaning that the variable is a constant and cannot be changed
const int x; // x is an integer constant
const double x; // x is a double constant

When const modifies a pointer:
const int * x; // x is a pointer to a constant integer
int * const x; // x is a constant pointer.
const int * const x; // x is a constant pointer and also points to a constant
x. To the right of * means a constant pointer, and to the left of * means a constant.


The iterators in STL are modeled with pointers, so the role of iterators is similar to T *.
When we use const to decorate an iterator:
const vector <int> :: iteator it; // it is a constant iterator, which cannot change the pointer to this iterator, but can change the value pointed to by this iterator.

If the things pointed to by the iterator cannot be changed, as follows:
vector <int> :: const_iterator it;


2、

const modified function parameter
 

<span style="font-size:14px;">int &fun(const int x);</span>
<span style="font-size:14px;"></span>

<span style="font-size:14px;">函数的形参是一个普通的const变量:
    1、 如果函数返回值一个non-const引用 , 那么就不能返回 x;
    2、 x 的值在函数中不能被改变 , 非常量指针不能指向它 , 非常量引用也不能引用该变量。</span>


<span style="font-size:14px;">int &fun(const int x)
{
    int *z = &x;    //错误的
    int *const z1 = &x;    //错误的,因为z1不是一个指向常量的指针
    const int *z2 = &x      //正确的
    int &z3 = x;        //错误的, 因为z3不是一个常量引用
    const int &z4 = x;       //正确的
    
    return x;  //错误的 , 不是一个常量引用
}</span>

If the formal parameter is a const pointer or const reference, it is the same as the above ordinary constant variable usage.

3. const modified member function

    Note: Only member functions can be modified by const, other functions can not be modified by const.
Points to note about const-modified member functions:
     1. In const member functions, all variables in the class contain the const attribute, and the const attribute of the pointer means that the pointer itself is a constant pointer.
     2. Therefore, in const member functions, non-const references and non-const pointers cannot be used, and variables in the class (except for the value pointed by the reference pointer)
     3. Two member functions can be overloaded if they are only different in const. of.

<span style="font-size:14px;">class xy
{
public:
    xy():z(new char[10])  {}
    xy(char *new_z)
    {
        int n = strlen(new_z);
        z = new char[n+1];
        strcpy(z , new_z);
    }
    const char &operator [](int si) const;
    char &operator [](int si);

private:
    char *z;
    int x;
    double y[10];
};

const char &xy::operator[](int si) const
{

    cout<<"const"<<endl;
    return *(z+si);
}

char &xy::operator[](int si)
{
    cout<<"no-const"<<endl;
    return const_cast<char &>(
        static_cast<const xy&>(*this)[si]
    );
}
</span>


In the class, there are often two functions overloaded through the const attribute. If these two functions have the same function, then in order to avoid code duplication, we often let the non-const version of the function call the const version of the function
as the above example The operator [] function in.

At this time, it is necessary to use const_cast and static_cast two types of forced transition

   const_cast: Remove the const attribute of the variable.
   const int x;
   int & y = const_cast <int &> (x);
   static_cast: forced conversion between ordinary variables, adding a const attribute to a variable
   int x;
   double y = static_cast <double> (x);


Published 190 original articles · 19 praises · 200,000+ views

Guess you like

Origin blog.csdn.net/zengchenacmer/article/details/38533221