Use const_cast on a const reference : const_cast « Data Types « C++ Tutorial






#include <iostream>
using namespace std;

void sqrval(const int &val)
{
  const_cast<int &> (val) = val * val;
}

int main()
{
  int x = 10;

  cout << "x before call: " << x << endl;
  sqrval(x);
  cout << "x after call: " << x << endl;

  return 0;
}
x before call: 10
x after call: 100








2.31.const_cast
2.31.1.const_cast int value
2.31.2.const_cast char type
2.31.3.Use const_cast on a const reference
2.31.4.Use const_cast to cast parameter before passing it into a function
2.31.5.Cast parameter with const_cast