From C to C++ Extra usage of const

        const is the abbreviation of constant, which means unchanging and constant. There is the const reserved keyword in C++, which can declare a constant. The compiler directly locks it during compilation. Its value cannot be modified and can only be read. Let’s explain its usage in detail below

The simplest usage

        When initializing a data or object, use the const keyword to turn it into a constant object. The value of a variable modified with const cannot be modified anywhere in the program and is used like a constant. Its value cannot be modified or assigned, it can only be read.

int a = 12;
const int a1 = 114514;
int const a2 = 114514;
a1 = 1919810;//错误,a1已被cosnt修饰,成为常量
a2 = 13;//cosnt在数据类型声明前后均可
a = 11;//普通变量的修饰
const int i;//错误,必须进行初始赋值
string s = "qwqwq";
string const s1 = "qaqaq";
const string s2 = "wqwqwq";
s = "123";//正确
s1 = "qqqqq";//错误
s2 = "swdadw";//错误

const reference        

        Const reference, you can create a cosnt reference to a const object, or you can create a const reference to a normal object, but you cannot create a normal reference to a const object. Because variables modified by const can only be read, it is obviously not advisable to use a normal reference that can be read or written to establish a relationship with a variable modified by const that can be read or written. However, a const reference established for an ordinary object can only be It can be read and cannot be modified. This feature can protect the original object and is commonly used in some functions.

    const int a1 = 114514;
    int const a2 = 114514;
    const int &b1 = a1;
    const int &b = a;
    int &b2 = a2;//非法 

         Then there are some features of const references, which can be bound to variables of different types but related (float and int are both numbers, but what exactly is related is being studied...); const can also be initialized to a literal value constant.

    float d = 3.14;//const引用可以绑定到类型不同,但类型相关的变量上
    const int &d1 = d;//正确,允许这样绑定
    int &d2 = d;//错误,普通引用无这样的特性
    const float &e = 3.1415;//const引用也可以直接对没有初始化的数据进行绑定
    float &f = 3.14159//错误, 普通引用无这种特性

        A reference stores the address of an object internally, which is an alias for the object. For non-addressable values, such as literal constants, and objects of different types, the compiler must generate a temporary object in order to implement the reference. The reference actually points to the object, but the user cannot access #Source Article #

const member function

Member functions can be declared as constants by adding the keyword const after the formal parameter list.

class student
{
    private:
        int num;
        int score;
    public:
        void display() const;
};
void student::display() const
{
    cout<<num<<endl;
}

Const member functions cannot change the data members of the object they operate, they can only be read, not modified;

Const members must appear in both the declaration and the function body and are part of the constant member function.

 For two member functions, the names and parameters are the same. Whether they are const will be regarded as overloaded .

const is used to protect shared data

constant data member

class student
{
    private:
        const int degree;
        int num;
        int score;
    public:
        void display() const;
};
void student::display() const
{
    cout<<num<<endl;
}

Add const before the data member to declare it as a constant data member. It can only be read but cannot be modified. It needs to be initialized with a parameter list.

constant object

class student
{
    private:
        const int degree;
        int num;
        int score;
    public:
        void display() const;
};
void student::display() const
{
    cout<<num<<endl;
}
int main()
{
    const student st1;
    student const st2;//const放在前放在后均可
}

After an object is declared as a constant object, only its constant member functions can be called , and internal data cannot be modified. All parameters must be brought during initialization.

Due to the existence of constant functions and constant objects, the data inside the const object is absolutely safe. But for some data that you want to modify, you can use the method of specifying variable data members.

mutable int i;

In this way, you can use const constant member functions to modify it.

constant pointer to object, pointer to constant object

constant pointer to object

//指向对象的常指针
time t1(19,12,14),t2;
time *const pl = &t1;

pointer to constant object

const char c[] = "boy";
const chat* p1 = c;

A constant pointer pointing to an object (const pointer): the value of the pointer does not change, and the pointer does not change.

Pointer to a constant object: The value of the class object it points to cannot be changed through the pointer. It is used for passing function parameters and protecting the object.

Summarize

 

Guess you like

Origin blog.csdn.net/Gelercat/article/details/127238428