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

#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 的全面总结



你可能感兴趣的:(C++,C++,Const)