const cast c++ code example
Example: C++ const_cast
// const_cast
#include <iostream>
using namespace std;
void print (char * str)
{
cout << str << '\n';
}
int main () {
const char * c = "sample text";
print ( const_cast< char *>(c) );
return 0;
}
// const_cast
#include <iostream>
using namespace std;
int fun(int* ptr)
{
return (*ptr + 10);
}
int main(void)
{
const int val = 10;
const int *ptr = &val;
int *ptr1 = const_cast<int *>(ptr);
cout << fun(ptr1);
return 0;
}