example2_using_const_cast_with_references.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream>

void printAndModify(const int& value) {
    std::cout << "Original value: " << value << std::endl;
    int& mutable_value = const_cast<int&>(value);
    mutable_value++;
    std::cout << "Modified value: " << value << std::endl;
}

int main() {
    int x = 5;
    printAndModify(x);
    return 0;
}
Back to constant_cast