Demonstrates const_cast : const cast « Development « C++






Demonstrates const_cast

Demonstrates const_cast

#include <iostream>
using namespace std;
void sqrval(const int *val)
{
  int *p;
                               
  p = const_cast<int *> (val); // cast away const-ness.
  *p = *val * *val;            // now, modify object through v
}
int main()
{
  int x = 10;
  cout << "x before call: " << x << endl;
  sqrval(&x);
  cout << "x after call: " << x << endl;
  return 0;
}

           
       








Related examples in the same category

1.Demonstrate const_cast.Demonstrate const_cast.
2.Use const_cast on a const reference.Use const_cast on a const reference.
3.const_cast Democonst_cast Demo