当前位置: 首页 > news >正文

广州网站建设 推广公司四大门户网站现状

广州网站建设 推广公司,四大门户网站现状,微信小程序开发需要什么,免费ppt模板资源C const关键字有多种用法 可以用来修饰变量、指针、函数参数、成员函数等。可以看到const在C中有多种用法#xff0c;主要用于保证数据的不可变性#xff0c;增强代码的安全性和可读性。在实际编程中#xff0c;根据需要选择适当的const用法#xff0c;可以有效避免意外修…C const关键字有多种用法 可以用来修饰变量、指针、函数参数、成员函数等。可以看到const在C中有多种用法主要用于保证数据的不可变性增强代码的安全性和可读性。在实际编程中根据需要选择适当的const用法可以有效避免意外修改数据提高程序的稳定性 以下是一些常见的用法及其示例 1. 修饰普通变量 const可以用来声明常量即变量的值在初始化后不能被修改。 #include iostreamint main() {const int num 10;// num 20; // Error: cannot assign to variable num with const-qualified type const intstd::cout num: num std::endl;return 0; }2. 修饰指针 const修饰指针时有多种情况取决于const的位置。 2.1 指向常量的指针 指针本身可以改变但不能通过该指针修改它所指向的值。 #include iostreamint main() {int value 10;const int *ptr value;// *ptr 20; // Error: read-only variable is not assignablevalue 20; // Allowed, since value itself is not constptr nullptr; // Allowed, ptr itself is not conststd::cout value: value std::endl;return 0; }2.2 常量指针 指针本身是常量但可以修改它所指向的值。 #include iostreamint main() {int value 10;int *const ptr value;*ptr 20; // Allowed, can modify the value pointed to// ptr nullptr; // Error: cannot assign to variable ptr with const-qualified type int *conststd::cout value: value std::endl;return 0; } 2.3 指向常量的常量指针 指针本身和指向的值都不能改变。 #include iostreamint main() {int value 10;const int *const ptr value;// *ptr 20; // Error: read-only variable is not assignable// ptr nullptr; // Error: cannot assign to variable ptr with const-qualified type const int *conststd::cout value: value std::endl;return 0; }3. 修饰函数参数 const可以用来修饰函数参数以保证函数内部不能修改参数的值。 3.1 按值传递的常量参数 这种情况虽然参数在函数内部不能修改但因为按值传递函数外部的变量不受影响。 #include iostreamvoid printValue(const int value) {// value 20; // Error: cannot assign to variable value with const-qualified type const intstd::cout Value: value std::endl; }int main() {int num 10;printValue(num);return 0; } 3.2 按引用传递的常量参数 这种情况既可以避免不必要的复制又保证了函数内部不能修改参数的值。 #include iostreamvoid printValue(const int value) {// value 20; // Error: cannot assign to variable value with const-qualified type const int std::cout Value: value std::endl; }int main() {int num 10;printValue(num);return 0; } 4. 修饰成员函数 const成员函数表示该函数不会修改对象的状态不能修改类的成员变量除非是用mutable关键字修饰的变量。 #include iostreamclass MyClass { public:int getValue() const {// value 20; // Error: cannot assign to non-static data member within const member functionreturn value;}void setValue(int v) {value v;}private:int value 10; };int main() {MyClass obj;std::cout Value: obj.getValue() std::endl;obj.setValue(20);std::cout Value: obj.getValue() std::endl;return 0; } 5. 顶层const和底层const 顶层constTop-level const指对象本身是常量例如 const int a 10;。 底层constLow-level const指对象的内容是常量例如 const int *ptr;。 6. 修饰返回类型 const可以修饰返回类型防止返回值被修改。 #include iostreamclass MyClass { public:const int getValue() const {return value;}private:int value 10; };int main() {MyClass obj;const int val obj.getValue();// val 20; // Error: cannot assign to variable val with const-qualified type const int std::cout Value: val std::endl;return 0; } 7. 修饰常量表达式 constexpr是C11引入的用于声明常量表达式保证表达式在编译时计算。 #include iostreamconstexpr int square(int x) {return x * x; }int main() {const int result square(5);std::cout Result: result std::endl;return 0; }
http://www.w-s-a.com/news/995270/

相关文章:

  • 郑州网站优化顾问济宁网站制作
  • 网站开发简单吗网站引导页分为三个板块设计风格
  • 湖南做网站 在线磐石网络百度一下百度搜索
  • 现在建网站多少钱推广营销费
  • 联想企业网站建设的思路西安网站建设阳建
  • 网站内容 内链网站建设电话销售工作总结
  • 系统网站开发知名的摄影网站有哪些
  • 网站拍照的幕布扬中网站建设价位
  • 网站ie兼容性差西安小程序开发的公司
  • 上海网站建设培训app网站开发成本
  • 个人网站icp外贸网站开发 河南
  • 遵义建设网站无锡市规划建设局网站
  • 海外留学网站建设方案门户网站的发布特点
  • 网站建设不赚钱net112企业建站系统
  • 网站建设团队管理模板贵州省住房和城乡建设部网站
  • 曲沃网站建设网上学编程的有哪些比较好的网站
  • 厦门网站建设慕枫学做网站需要多久
  • 爱奇艺做任务领vip网站设计广告图片
  • 中科汇联网站建设手册上海公司名称注册查询网
  • 网站建设电子商务课总结和体会关于做网站书籍
  • 仪征网站建设公司哪家好简单网页制作素材图片
  • 甘肃第九建设集团公司网站潍坊个人做网站
  • 如何做后台网站的教程网站建设 关于我们
  • 极速网站建设哪家好连云港百度推广网站建设
  • 医院网站建设的目标wordpress中英文网站模板
  • 门户型网站开发难度网站导航栏有哪些
  • 推荐做任务网站软件定制开发哪家好
  • 邯郸兄弟建站第三方仓储配送公司
  • 商丘家具网站建设wordpress 添加代码
  • 基础建设的网站有哪些内容成都科技网站建设咨询电话