#include <iostream> using namespace std; void sqrval(const int *val) { int *p; // cast away const-ness. p = const_cast<int *> (val); *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; }
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 |