Easy Tutorial
❮ Android Tutorial Absolutelayout Android Tutorial Framelayout ❯

Summary of the C++ const Keyword

Category Programming Techniques

const is an abbreviation of constant, which means unchanging or not easily changed. In C++, it is used to modify built-in type variables, custom objects, member functions, return values, and function parameters.

C++ const allows the specification of a semantic constraint, which the compiler will enforce, allowing programmers to tell the compiler that a certain value remains unchanged. If there is indeed a value that remains unchanged in programming, const should be explicitly used to gain the compiler's assistance.

1. const Modifier for Ordinary Type Variables

const int a = 7;
int b = a; // Correct
a = 8;       // Error, cannot change

a is defined as a constant, and a can be assigned to b, but a cannot be reassigned. Assigning a value to a constant is illegal because a is considered by the compiler as a constant, and its value is not allowed to be modified.

Next, let's look at the following operation:

Example

#include<iostream>

using namespace std;

int main(void)
{
    const int a = 7;
    int *p = (int*)&a;
    *p = 8;
    cout << a;
    system("pause");
    return 0;
}

For the const variable a, we take the address of the variable and convert it to a pointer pointing to int, then use *p = 8; to reassign the value in the address of variable a, and then output to check the value of a.

From the debugging window below, we can see that the value of a has been changed to 8, but the output result is still 7.

From the results, we can see that the compiler still considers the value of a to be the originally defined 7, so the operation on const a will produce the above situation. So do not easily try to assign a value to a const variable, as this will produce unexpected behavior.

If you do not want the compiler to detect the above operation on const, we can add the volatile keyword in front of const.

The volatile keyword is the opposite of const, meaning changeable and easily changed. Therefore, it will not be optimized by the compiler, and the compiler will not change the operation on the variable a.

Example

#include<iostream>

using namespace std;

int main(void)
{
    volatile const int a = 7;
    int *p = (int*)&a;
    *p = 8;
    cout << a;
    system("pause");
    return 0;
}

The output result is as we expect, 8.

2. const Modifier for Pointer Variables.

There are three situations for const to modify pointer variables.

For A:

const int *p = 8;

The content pointed to by the pointer, 8, cannot be changed. It is referred to as left-value binding because const is located on the left side of the *.

For B:

int a = 8;
int* const p = &a;
*p = 9; // Correct
int b = 7;
p = &b; // Error

For the const pointer p, the memory address it points to cannot be changed, but its content can be changed. It is referred to as right-value binding because const is located on the right side of the *.

For C: It is a combination of A and B

int a = 8;
const int *const p = &a;

At this time, both the content pointed to by const p and the memory address it points to are fixed and cannot be changed.

For A, B, and C, according to the position of const on the *, I summarize three sentences for easy memorization: "Left-value binding, right-value binding, const modifies unchangeable variables".

3. const Parameter Passing and Function Return Values.

There are three situations for const to modify function parameters.

A: const modifies the value passed by value, generally, const modification is not needed in this situation because the function will automatically create a temporary variable to copy the actual parameter value.

Example

```

include<iostream>

using namespace std;

void Cpf(const int a) { cout << a; // ++a; is incorrect, a cannot be changed }

int main(void)

{ Cpf(

❮ Android Tutorial Absolutelayout Android Tutorial Framelayout ❯