Use const_cast on a const reference. : const cast « Development « C++






Use const_cast on a const reference.

Use const_cast on a const reference.

#include <iostream>
using namespace std;

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

int main()
{
  int x = 22;

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

  return 0;
}


           
       








Related examples in the same category

1.Demonstrates const_castDemonstrates const_cast
2.Demonstrate const_cast.Demonstrate const_cast.
3.const_cast Democonst_cast Demo