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
*/

 

posted @ 2017-02-16 15:37  sky20080101  阅读(120)  评论(0编辑  收藏  举报