SoFunction
Updated on 2025-04-12

Detailed description and introduction to the use of keyword const in C++ (most complete)

In C++,constis a very important keyword used to declare constants, modify variables, parameters and member functions. Here's aboutconstDetailed description:

1. Constant declaration

useconstKeywords can declare constants, and their values ​​cannot be modified once initialized.

const int MAX_SIZE = 100;
const double PI = 3.14159;

2. Constant Modification Variables

By adding a variable declarationconstKeywords, you can declare this variable as a constant so that its value cannot be modified after initialization.

const int x = 10;

3. Constant reference

constIt can also be used to modify quotations.Such references can only be bound to constant objects of the same type

const int& ref = x; // Constant reference

This code declares a constant referenceref, which references the integer constant defined earlierx. Because it is a constant reference, it cannot be passedrefRevisexvalue.

4. Constant pointer

A pointer to a constant cannot modify the value pointed to by a pointer, but it can be modified to point to other objects.

int y = 20;
const int* ptr = &y; // Pointer to constant

This code declares an integer variabley, and then declare a pointer to an integer constantptr, it points to the variabley. becauseptrIt is a pointer to a constant, so it cannot be passedptrReviseyvalue.

5. Constant member functions

In the class, add the member function after itconstA keyword indicates that the member function does not modify the member variables of the class.

class MyClass {
public:
    void func() const; // Constant member functions};

This code defines a classMyClass, where a constant member function is declaredfunc(). Add at the end of the function declarationconstKeyword, indicating that the member function will not modify the member variable of the class.

6. Constant Objects

After creating a constant object, its member variables cannot be modified.

class MyClass {
public:
    int x;
};

const MyClass obj; // Constant object// = 10; // Error, the member variable of the constant object cannot be modified

7. Constant member variables

In the class, useconstModified member variables can only be initialized in the initialization list and cannot be modified in the constructor.

class MyClass {
public:
    const int x;
    MyClass(int value) : x(value) {} // Constant member variable initialization};

This code defines a classMyClass, there is an integer constant member variablex. In the initialization list of the constructorxInitialize becausexIt is a constant member variable, so it can only be initialized in the initialization list, and its value cannot be modified inside the constructor.

8. Initialization of constant objects

Constant objects must be initialized at declaration time, otherwise a compilation error will be raised.

const int value = 100; // Correct, constant initialization// const int value; // Error, constant declaration not initialized

9. Constant Expressions

useconstDeclared variables or constants can be used for compilation-time calculations, thereby generating constant expressions.

const int arraySize = 10;
int myArray[arraySize]; // Use constant expressions to define the array size

10. const and pointer

constIt can be used with pointers to form pointers to constants, constant pointers, etc.

const int* ptr1; // Pointer to constantint* const ptr2; // Constant pointerconst int* const ptr3; // Constant pointer, pointer to constant

Here we will explain the specific usage and illegal operations in detail
When we use pointers in C++,constCan be used in conjunction with pointers to provide more constraints and semantic clarity. Below I will explain in detail the content related to const and pointers, including usage and unsubmitted operations.

10.1 Pointer to a constant

const int* ptr1;

This is a pointer to a constant. This means pointerptr1Can point to aconst intVariable of type, but cannot passptr1Modify the value pointed to. For example:

const int x = 10;
ptr1 = &x; // Legal, pointer points to the constant x*ptr1 = 20; // Illegal, the value of x cannot be modified through ptr1

10.2 Constant pointer

int* const ptr2;

This is a constant pointer that points to an integer variable, and the pointer itself is a constant and cannot be modified. This means that the value of the pointer itself (i.e. the address to which the pointer points) cannot be changed, but the value it points to can be modified through the pointer. For example:

int y = 20;
ptr2 = &y; // Illegal, constant pointer cannot modify the value of the pointer*ptr2 = 30; // Legal, the value of y can be modified through ptr2

10.3 Constant pointer, pointer to constant

const int* const ptr3;

This is a constant pointer, pointer to a constant. It satisfies the constraints of the first two cases at the same time: the pointer itself is a constant, and the value pointed to is also a constant. In this case, the address of the pointer cannot be modified, nor can the value pointed to be modified through the pointer. For example:

const int z = 30;
ptr3 = &z; // Illegal, constant pointer cannot modify the value of the pointer*ptr3 = 40; // Illegal, cannot modify the value of z through ptr3

The above is the detailed description and introduction of the keyword const in C++ (the most complete) detailed content. For more information on the use of the keyword const in C++, please pay attention to my other related articles!