C + + foundation -- const qualifier

C + + foundation -- const qualifier

1. const qualifier

Const qualifier means that the modification type is constant, that is, the variable modified with const qualifier is an immutable quantity, which can act on many types, such as basic type, structure type, pointer, etc

const modifies the basic variable type

const modifies the basic variable type, which means that the modified variable has and can only be initialized once

int main(){ 
    const int a; //If initialization is not carried out, an error will be reported and initialization must be carried out
    return 0;
}

int main(){
    const int a = 10; //Initialize at definition time
    const int b;
    b = 10;
    return 0;
}

int main(){
    const int a = 10;
    float const b = 10.0; //Variable type can be in front of const or after const
    a = 20; //Cannot assign value after initialization
    b = 20.0;
    return 0;
}

When there are variables that do not need to be modified, you can modify them with const to avoid the modification of these variables by special operations. For example, the zip code of each region of the country is a fixed value and does not need to be modified. In order to prevent these variables from being modified by improper operations, you can modify them with const qualifier or enumeration, This is just an example

#include<iostream>
const int POST_CODE_TO_CHANGSHA = 0; //The zip code of Changsha represents the value of
const int POST_CODE_TO_BEIJING = 1; //Beijing
const int POST_CODE_TO_SHANGHAI = 2; //Shanghai
int main(){
    int a;
    std::cout<<"Please input the postcode:";
    std::cin>>a;
    switch (a) {
        case POST_CODE_TO_CHANGSHA:{
            std::cout<<"The postcode that you input is ChangSha\n";
            break;
        }
        case POST_CODE_TO_BEIJING:{
            std::cout<<"The postcode that you input is BeiJing\n";
            break;
        }
        case POST_CODE_TO_SHANGHAI:{
            std::cout<<"The postcode that you input is ShangHai\n";
            break;
        }
        default:{
            std::cout<<"The postcode that you input is incorrectly\n";
        }
    }
    return 0;
}

const is placed after the method

class Test01{
    int a;
    int b;
public:
    Test01(int a,int b):a(a),b(b){}

    void setA(int a) const{ //const is placed after the method
        this->a = a; //This industry will report mistakes,
    }
    void setB(int b){
        this->b = b;
    }
    void set() const{
        setB(3);
        setA(3);
    }
};

After the const modified method, it indicates that this method cannot change the variables in the class, and non const methods cannot be called in the const modified method. However, if a mutable keyword is added in front of the attribute definition, it will have different effects

class Test01{
    mutable int a;
    int b;
public:
    Test01(int a,int b):a(a),b(b){}

    void setA(int a) const{ //const is placed after the method
        this->a = a; //This industry will report mistakes,
    }
    void setB (int b){
        this->b = b;
    }
};

cv (const and volatile) type qualifier - cppreference com

If you can see this, you won't report an error!

As for the reason, I searched the information

[from stackoverflow]( c++ - How to call a non-const method from a const method? - Stack Overflow)

Of course, you can use const to modify variables in const method_ Cast forcibly converts the pointer of this into a pointer to the object itself, and changes the value of the variable through the operation of this pointer

class Test01{
    int a;
    int b;
public:
    Test01(int a,int b):a(a),b(b){}

    void setA(int a) const{ //const is placed after the method
        Test01* me = const_cast<Test01*>(this);
        me->a = a; //The obtained migration pointer can be used to change the value of non const variables in const modified methods and call non const modified methods
        me->setB(10); 
    }
    void setB (int b){
        this->b = b;
    }
};
//This idea is more intuitive, but a small disadvantage is that the actual consumption of space becomes larger. A big disadvantage is that it can not guarantee that the data of other members will not be modified

const pointer and pointer to const

Literally speaking, const pointer refers to a pointer variable whose address cannot point to other addresses once initialized, and pointer to const is a pointer to a constant. For the pointer variable itself, it is non const, but the value it points to can be const modified or not. See the code above

int main(){
    int a = 10,b = 20;
    const int c = 30;
    //const pointer is a constant pointer, and the pointer variable itself is a constant
    int *const p = &a; //Must be initialized at definition time,
    //p = &b;  If you assign a value to p again, an error will be reported. P is a constant pointer. It can only point to a unique address and can no longer be changed. This is from the beginning to the end!

    //pointer to const pointer constant, pointer variable pointing to constant
    const int *q; //Pointer constants can be uninitialized or ignored
    q = &a;
    q = &b;
    q = &c; //pointer to const is strange. How can it point to non const variables? In fact, it is a little overconfident. It thinks it points to constants, so it appears
    //Operation similar to ordinary pointer
    //int *r = &c; // The non constant pointer cannot point to a constant, so the paper will report an error
    return 0;
}

In fact, let's sum it up

  1. const pointer, the pointer variable itself is a constant. The address it points to can only be initialized once, and must be initialized once defined
  2. pointer to const means that the pointer variable itself is not a constant. It can point to a constant. What you point to for yourself is a constant. You don't even look at it, so you can assign values multiple times and point to different addresses!

It is suggested to understand it in English, otherwise the Chinese definition is a little windy

Keywords: C++ pointer

Added by yozyk on Thu, 10 Feb 2022 15:07:15 +0200