const基础(1)

来源:互联网 发布:支付宝修改淘宝会员名 编辑:程序博客网 时间:2024/06/06 05:29
struct Teacher{    char name[64];    int age;};int operatorTeacher(const Teacher *pT)//const Teacher *pT指针指向的内存空间不能被修改{    pT->age = 10;//修改了指针指向的空间    return 0;}

运行结果:
error C3490: 由于正在通过常量对象访问“age”,因此无法对其进行修改;

#include<iostream>using namespace std;struct Teacher{    char name[64];    int age;};//int operatorTeacher01(const Teacher *pT)//const Teacher *pT指针指向的空间不能被修改//{//  //pT->age = 10;//修改了指针指向的空间//  return 0;//}int operatorTeacher02( Teacher * const pT)//const Teacher *pT指针指向的空间不能被修改{    pT->age = 10;//修改了指针指向的空间但是pT不能被修改    return 0;}void main(){    //const int a;    //int const b;//前两种写法相同    //const int *c;//const修饰的是指针指向的空间    //int *const d;//const修饰的是指针变量本身    //const int * const e;//指针变量和质量变量修饰的值不能修改    cout<<"hello..."<<endl;    system("pause");    return;}
原创粉丝点击