Summary of const Keyword Usage in C++

Words: 893

When modifying with const, there are three different effects depending on the position of const.

The criterion is: whatever const modifies, the content of that thing (after const) is immutable.

Modifying variables

const int a; means the variable cannot be changed.

Modifying pointers

Divided into pointers to constants (pointer to const) and pointers that are themselves constants (constant pointer, const pointer).

const int *p = &a;

*p is immutable. *p is the content of the memory location pointed to by p, so *p being immutable means the content of the memory location pointed to by p is immutable. p is mutable, meaning the memory location (variable) pointed to by p can be changed, but whatever p points to, that value is immutable.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using namespace std;
int main(){

    int a = 10;
    const int *p = &a; //不可以用*p改变a的值
    printf("a = %d \n", a);
    //*p = 20; //语法错误,*p不可被修改
    a = 40; //可以直接赋值进行修改
    printf("a = %d \n", a);

    int b = 11;
    p = &b;
    printf("b = %d \n", *p);

    int *q = &a;
    *q = 20; //可以用*q改变a的值
    printf("a = %d \n", a);
    a = 30; //给a赋值也可以修改a的值
    printf("a = %d \n", a);

    return 0;
}

>a = 10 
a = 40 
b = 11
a = 20
a = 30

int* const p = &a;

const modifies p, so the pointer p is immutable, meaning the memory location pointed to by p cannot be changed. The pointing of p is immutable, but the content of the memory location pointed to by p can change.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;
int main(){

    int a = 10;
    int* const p = &a; //p的指向不可变
    printf("a = %d \n", a);

    *p = 20; //可以修改p指向的内存单元的内容
    printf("a = %d \n", a);

    int b = 11;
    //p = &b; //语法错误
    printf("b = %d \n", b);

    *p = b; //修改了a的值
    printf("a = %d \n", a);

    return 0;
}

>a = 10 
a = 20
b = 11
a = 11

const int* const p = &a;

const modifies both *p and p, so both the memory location pointed to by p and the content of the memory location pointed to by p are immutable.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;
int main(){

    int a = 10;
    const int* const p = &a; //const修饰了p和*p
    printf("a = %d \n", a);

    //*p = 20; //不可修改p指向的内存单元的内容
    printf("a = %d \n", *p);

    int b = 11;
    //p = &b; //不可修改p的指向
    printf("b = %d \n", b);

    return 0;
}

>a = 10 
a = 10
b = 11

Modifying references

A reference to a constant (reference to const), used for parameter types, which avoids both copying and modification of the value by the function;

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;
int main(){

    int a = 10;
    int &r = a; //定义了a的引用r
    printf("a = %d \n", r);
    r = 20; //也修改了a的值
    printf("a = %d \n", a);

    //int &rm = 3; //语法错误,引用不可直接绑定常量
    const int &rm = 3;
    printf("&rm = %d \n", rm); //常量可以绑定const引用
    /* 编译器分配内存空间,把rm放在符号表中
    int temp = 3;
    const int &rm = temp;
    */

    int b = 5;
    const int &rb = b;
    printf("b = %d \n", rb);

    //rb = 6; //const修饰引用不可更改

    double p = 3.1415;
    //int &rp = p; //引用类型需与绑定类型相同
    const int &rp = p; //可以绑定到const引用上
    printf("&rp = %d \n", rp); //常量可以绑定const引用
    /*
    double p = 3.1415;
    int temp = p;
    const int &rp = temp;
    */

    return 0;
}

>a = 10 
a = 20
&rm = 3
b = 5
&rp = 3

The purpose of a const reference is to prohibit changing the referenced object by modifying the reference value.

There is no const reference, because a reference itself is a const pointer.

  • An ordinary reference is equivalent to int* const e
  • const int &e is equivalent to const int* const e

Modifying member functions

Indicates that member variables cannot be modified within this member function.

  • A member function modified with const can only read data members; a member function without const can read and write data members.
  • A const object can call const member functions and cannot call functions not modified with const.
cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
class Test {
public:
    Test(int v) : a(v) {}
    int getA() { return a; }
    int getAc() const { return a; }

    int a;
};
int main() {
    Test test(2);
    cout << test.getA() << endl;
    cout << test.getAc() << endl;
    return 0;
}

>2
2

This is equivalent to C++ hiding the this pointer in the class’s member functions. In essence, const modifies the memory space pointed to by the hidden this pointer; it modifies the this pointer (const Class *this).

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...