自考新教材--p35

源程序:

#include <iostream>

using namespace std;

int main()

{

int a = 10;

const int *p = &a;

const int ca = 30;

int *q;

cout << "a的地址为:\t" << &a << "\ta的值为:\t" << a << endl;

cout << "*p指向的地址为:" << p << "\t*p的值为:\t" << *p << endl;

q = const_cast<int *>(p);  //去除p的常量性赋给q, 如果写p=q,则会报错

*q = 20;

cout << "a的地址为:\t" << &a << "\ta的值为:\t" << a << endl;

cout << "*p指向的地址为:\t" << p<< "\t*p的值为:\t" << *p << endl;

cout << "*q指向的地址为:\t" << q << "\t*p的值为:\t" << *q << endl;

cout << "分界线" << endl;

p = &ca;   //ca的值不能修改

q = const_cast<int *>(p);  //去除p的常量性赋给q

*q = 40;

cout << "ca的地址为:\t" << &ca << "\tca的值为:\t" << ca << endl;

cout << "*p指向的地址为:\t" << p << "\t*p的值为:\t" << *p << endl;

cout << "*q指向的地址为:\t" << q << "\t*p的值为:\t" << *q << endl;

system("pause");

return 0;

}

运行结果:

posted @ 2019-12-02 10:35  bobo哥  阅读(136)  评论(0编辑  收藏  举报