收藏 分销(赏)

二级考试C++基础-C++中CONST的用法.docx

上传人:a199****6536 文档编号:6491422 上传时间:2024-12-09 格式:DOCX 页数:3 大小:13KB 下载积分:5 金币
下载 相关 举报
二级考试C++基础-C++中CONST的用法.docx_第1页
第1页 / 共3页
二级考试C++基础-C++中CONST的用法.docx_第2页
第2页 / 共3页


点击查看更多>>
资源描述
二级考试C++基础:C++中CONST的用法 const char *const * keywords   const char const * keywords   const char *const keywords   const char const keywords   下面总结一下CONST的用法。const主要是为了程序的强健型,削减程序出错。考试,大提示const指针和引用一般用在函数的参数中。最根本的用法如下:   // a的内容不变,a只能是100也就是声明一个int类型的常量(#define b =100)   const int a=100;   int const b=100; //和上面作用一样   //下面两句话一样   const int b=100;   int const b=100;   const放在*左侧修饰的是指针的内容,const放在*右侧修饰的是指针本身。如下:   const int* pa = //指针指向的内容为常量(就是a的值不变)   int const *a = //指针指向的内容为常量(就是b的值不变)*p=3//error   int* const a = //指针为常量,不能更改指针了如 a++但可以改值*a=3;   const int* const a =   void fun(const int* const a){}   fun(init)//爱护参数不被修改   CONST的主要功能就是爱护函数的参数,使其在调用时不被修改:   int A(int)const; //是常函数,只能用在类中,调用它的对象不能改转变成员值   const int A(); //返回的是常量,所以必需这么调用 cosnt int a=A();   int A(const int); //参数不能改值,可用在任意函数   int height() const;//常函数只能由常函数调用   int max(int,int) const;   int Max = max(height(),height());   因此上面的问题迎刃而解了。const放在*左侧修饰的是指针的内容,const放在*右侧修饰的是指针本身。   const char *const * keywords ----keywords 是一个一般的指针,它指向一个指向常量的常量指针   const char const * keywords ----与const char *keywords 或char const *keywords 等同,keywords是指向常量的一般指针   const char *const keywords ----keywords是指向常量的常量指针,无论是它的值还是它指向的地址空间的值都不能更改   const char const keywords ----与const char keywords 或 char const keywords等同, 定义了一个字符常量keywords 。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服