const小练习一则,把const int*去除常量性赋给int*

来源:互联网 发布:mac系统上好玩的游戏 编辑:程序博客网 时间:2024/05/18 00:47
#include<iostream>using namespace std;int main(){  const int a=10; // a=1000;//error: assignment of read-only variable `a'   int b=a;  b=a+10;  const int *pInt=&b;  cout<<"*pInt: "<<*pInt<<endl;  pInt= &a;  cout<<"*pInt: "<<*pInt<<endl;  b=100;  pInt = &b;   cout<<"*pInt: "<<*pInt<<endl;  //int *p=&a;//error: invalid conversion from `const int*' to `int*'   int *p = (int*)&a;//强制类型转换,去除常量性   cout<<"*p: "<<*p<<endl;   //a=1000;//error: assignment of read-only variable `a'   getchar();  return 0;}

结果如下:

相关:

const 用法总结

关于C++ const 的全面总结



原创粉丝点击