const_cast

#include <string>
#include <iostream>
using namespace std;

void changeFirst(char* c)
{
    c[0]+=1;
}

int main(int argc, char ** argv_)   
{  
 int i = 100;  
 int *j = &i;  
 const int *k = const_cast<const int*>(j);  
 //const int *m = j;   感觉和这样写差不多  
  
 //指的地址都一样  
 cout <<i<<","<<&i<<endl; //100, 0012FF78  
 cout <<*j<<","<<j<<endl; //100, 0012FF78  
 cout <<*k<<","<<k<<endl; //100, 0012FF78  
  
// *j = 200;  
// *k = 200;   //error  
 string str("Hello");
 changeFirst(const_cast<char*>(str.c_str()));
 cout<<str.c_str()<<endl;

 return 0;  
}  
/*
100,0012FF38
100,0012FF38
100,0012FF38
Iello
Press any key to continue
*/
原文地址:https://www.cnblogs.com/sky20080101/p/6405996.html