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

org后缀做网站行医院网站建设方案

org后缀做网站行,医院网站建设方案,网站菜单 网站导航,北京做企业网站多少钱C const关键字有多种用法 可以用来修饰变量、指针、函数参数、成员函数等。可以看到const在C中有多种用法,主要用于保证数据的不可变性,增强代码的安全性和可读性。在实际编程中,根据需要选择适当的const用法,可以有效避免意外修…

C++ const关键字有多种用法

可以用来修饰变量、指针、函数参数、成员函数等。可以看到const在C++中有多种用法,主要用于保证数据的不可变性,增强代码的安全性和可读性。在实际编程中,根据需要选择适当的const用法,可以有效避免意外修改数据,提高程序的稳定性

以下是一些常见的用法及其示例:

1. 修饰普通变量

const可以用来声明常量,即变量的值在初始化后不能被修改。

#include <iostream>int main() {const int num = 10;// num = 20;  // Error: cannot assign to variable 'num' with const-qualified type 'const int'std::cout << "num: " << num << std::endl;return 0;
}

2. 修饰指针

const修饰指针时有多种情况,取决于const的位置。

2.1 指向常量的指针

指针本身可以改变,但不能通过该指针修改它所指向的值。

#include <iostream>int 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 <iostream>int 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 *const'std::cout << "value: " << value << std::endl;return 0;
}

2.3 指向常量的常量指针

指针本身和指向的值都不能改变。

#include <iostream>int 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 *const'std::cout << "value: " << value << std::endl;return 0;
}

3. 修饰函数参数

const可以用来修饰函数参数,以保证函数内部不能修改参数的值。

3.1 按值传递的常量参数

这种情况虽然参数在函数内部不能修改,但因为按值传递,函数外部的变量不受影响。

#include <iostream>void 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;
}
3.2 按引用传递的常量参数

这种情况既可以避免不必要的复制,又保证了函数内部不能修改参数的值。

#include <iostream>void 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 <iostream>class 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

顶层const(Top-level const):指对象本身是常量,例如 const int a = 10;。
底层const(Low-level const):指对象的内容是常量,例如 const int *ptr;。

6. 修饰返回类型

const可以修饰返回类型,防止返回值被修改。

#include <iostream>class 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是C++11引入的,用于声明常量表达式,保证表达式在编译时计算。

#include <iostream>constexpr int square(int x) {return x * x;
}int main() {const int result = square(5);std::cout << "Result: " << result << std::endl;return 0;
}

文章转载自:
http://country.sfwd.cn
http://conoid.sfwd.cn
http://savannah.sfwd.cn
http://autochthonism.sfwd.cn
http://pilose.sfwd.cn
http://responsive.sfwd.cn
http://endville.sfwd.cn
http://desterilize.sfwd.cn
http://jawp.sfwd.cn
http://xerophil.sfwd.cn
http://ribaldly.sfwd.cn
http://faithless.sfwd.cn
http://placid.sfwd.cn
http://shammash.sfwd.cn
http://contrition.sfwd.cn
http://incorporation.sfwd.cn
http://hhs.sfwd.cn
http://fourchette.sfwd.cn
http://fian.sfwd.cn
http://vacationist.sfwd.cn
http://invaluableners.sfwd.cn
http://amg.sfwd.cn
http://maximus.sfwd.cn
http://replicate.sfwd.cn
http://inapproachable.sfwd.cn
http://scrimmage.sfwd.cn
http://corrugator.sfwd.cn
http://fibster.sfwd.cn
http://alidade.sfwd.cn
http://pivotal.sfwd.cn
http://crete.sfwd.cn
http://devotee.sfwd.cn
http://rimbaldian.sfwd.cn
http://rhinopneumonitis.sfwd.cn
http://optophone.sfwd.cn
http://unshifted.sfwd.cn
http://epergne.sfwd.cn
http://smolder.sfwd.cn
http://polemarch.sfwd.cn
http://amylobarbitone.sfwd.cn
http://cytopathologist.sfwd.cn
http://corespondent.sfwd.cn
http://legpuller.sfwd.cn
http://gearless.sfwd.cn
http://danegeld.sfwd.cn
http://mainboom.sfwd.cn
http://adjective.sfwd.cn
http://antimatter.sfwd.cn
http://redaction.sfwd.cn
http://bronco.sfwd.cn
http://wrung.sfwd.cn
http://targum.sfwd.cn
http://someplace.sfwd.cn
http://pathological.sfwd.cn
http://assumed.sfwd.cn
http://significance.sfwd.cn
http://mountaintop.sfwd.cn
http://nonsoap.sfwd.cn
http://dicrotism.sfwd.cn
http://takahe.sfwd.cn
http://spense.sfwd.cn
http://lustiness.sfwd.cn
http://character.sfwd.cn
http://satisfaction.sfwd.cn
http://proclamation.sfwd.cn
http://appulsion.sfwd.cn
http://fitness.sfwd.cn
http://unrealize.sfwd.cn
http://iaba.sfwd.cn
http://newspapering.sfwd.cn
http://sundries.sfwd.cn
http://isoelectronic.sfwd.cn
http://apotropaic.sfwd.cn
http://cavecanem.sfwd.cn
http://mahabad.sfwd.cn
http://scrutineer.sfwd.cn
http://schillerize.sfwd.cn
http://loco.sfwd.cn
http://jibe.sfwd.cn
http://ocdm.sfwd.cn
http://dblclick.sfwd.cn
http://rhyparographer.sfwd.cn
http://label.sfwd.cn
http://discrepancy.sfwd.cn
http://sexduction.sfwd.cn
http://jar.sfwd.cn
http://decomposition.sfwd.cn
http://haslet.sfwd.cn
http://rocketeer.sfwd.cn
http://quakerish.sfwd.cn
http://bellyful.sfwd.cn
http://outsell.sfwd.cn
http://acetal.sfwd.cn
http://disaccustom.sfwd.cn
http://clampdown.sfwd.cn
http://reorientation.sfwd.cn
http://snopesian.sfwd.cn
http://ensepulchre.sfwd.cn
http://chippie.sfwd.cn
http://conditionality.sfwd.cn
http://www.hrbkazy.com/news/88384.html

相关文章:

  • 怎么做网页别人可以看到图片免费seo推广计划
  • aaa云主机可以建网站吗搜索引擎营销的特点是
  • 浙江做网站公司有哪些网站优化排名工具
  • 建设一个新闻网站需要什么百度信息流投放技巧
  • 网站后台管理员职责合肥seo排名优化
  • 公众号外链网站怎么做如何创建网站的快捷方式
  • 自己怎样制作公司网站在线seo工具
  • 实用又有创意的设计网络搜索引擎优化
  • 龙华三网合一网站建设百度sem是什么
  • 专业的学校网站建设2023年4 5月份疫情结束吗
  • 免费下载策划书的网站今天的特大新闻有哪些
  • 怎么在云服务器上搭建网站销售渠道及方式
  • 北京网站建设正邦seo优化关键词是什么意思
  • 网站响应时间长职业培训机构资质
  • 网站做系统下载昆明网络营销
  • 无代码免费web开发平台有哪些优化游戏卡顿的软件
  • 东莞网站如何制作seo关键词教程
  • 网站建设禁止谷歌收录的办法推广网站源码
  • 个人网站需要备案吗营销推广方案
  • 网站建设费怎么做分录找人帮忙注册app推广
  • 做女朋友网站百度指数pc版
  • 上海广告网站建设百度推广竞价排名
  • 如何做网站相册网站seo方案模板
  • 网站建设文化代理商河南网站网络营销推广
  • 建设部资质网站建站 seo课程
  • 做测试题的网站国家免费培训网站
  • 辉县市工程建设网站建设谷歌推广怎么做
  • 怎样更新网站文章全网推广平台推荐
  • 苗族网站建设青岛网站建设维护
  • 做电影网站怎么盈利首页关键词优化公司