C++ const Keyword

The const keyword in C++ is used to declare variables, pointers, function parameters, return types, and member functions as constant. Once declared as const, the value or behavior of these entities cannot be modified. This ensures data immutability, enhances code safety, and helps avoid unintended side effects.

The const keyword can be applied in multiple scenarios, such as declaring read-only variables, protecting function parameters from modification, and ensuring class member functions do not alter the object state.


Syntax

</>
Copy
// Constant variable
const data_type variable_name = value;

// Constant function parameter
return_type function_name(const data_type parameter);

// Constant member function
return_type class_name::function_name() const;

// Constant pointer
const data_type* pointer_name;
// or
data_type* const pointer_name;
const
The keyword that makes the variable, pointer, or function immutable.
data_type
The type of the variable or pointer.
variable_name
The name of the variable declared as constant.
parameter
A constant parameter passed to a function, ensuring its value is not modified inside the function.

Examples

Example 1: Declaring a Constant Variable

This example demonstrates how to declare and use a constant variable.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    const int MAX_USERS = 100; // Constant variable
    cout << "Maximum Users: " << MAX_USERS << endl;

    // Uncommenting the following line will cause a compilation error
    // MAX_USERS = 200;

    return 0;
}

Output:

Maximum Users: 100

Explanation:

  1. The variable MAX_USERS is declared as const, meaning its value cannot be modified after initialization.
  2. Attempting to assign a new value to MAX_USERS will result in a compilation error.

Example 2: Constant Function Parameters

This example demonstrates using the const keyword with function parameters.

</>
Copy
#include <iostream>
using namespace std;

void printMessage(const string& message) {
    cout << "Message: " << message << endl;

    // Uncommenting the following line will cause a compilation error
    // message = "New Message";
}

int main() {
    string msg = "Hello, World!";
    printMessage(msg);
    return 0;
}

Output:

Message: Hello, World!

Explanation:

  1. The function printMessage takes a constant reference to a string as its parameter.
  2. The parameter message cannot be modified inside the function, ensuring its immutability.
  3. The original msg is passed to the function without being altered.

Example 3: Constant Member Function

This example demonstrates a const member function in a class.

</>
Copy
#include <iostream>
using namespace std;

class User {
private:
    string name;

public:
    User(string userName) : name(userName) {}

    void displayName() const {
        cout << "User Name: " << name << endl;

        // Uncommenting the following line will cause a compilation error
        // name = "New Name";
    }
};

int main() {
    User user("Alice");
    user.displayName();
    return 0;
}

Output:

User Name: Alice

Explanation:

  1. The displayName method is declared as const, ensuring it cannot modify the object’s state.
  2. Attempting to modify the private member name inside displayName will result in a compilation error.

Key Points about const Keyword

  1. The const keyword ensures immutability and protects variables, parameters, and functions from unintentional modifications.
  2. Constant variables must be initialized at the time of declaration and cannot be reassigned later.
  3. const function parameters prevent modification of the passed argument within the function.
  4. const member functions guarantee that they do not alter the state of the object.
  5. Using const enhances code safety, readability, and robustness in C++ programming.