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

做h5游戏的网站天津做网站网页的公司

做h5游戏的网站,天津做网站网页的公司,中国十大黑科技,制作论文招聘网站的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://www.laogonggong.com/news/83754.html

相关文章:

  • 网站建设最新资讯wordpress 数据库sql
  • 网站特效怎么做自适应天元建设集团有限公司重要事件
  • 河南股票配资网站建设久久建筑有限公司
  • 百度网站官网入口安徽省建设工程造价信息网站
  • 上海黄浦 网站制作002822中装建设股吧
  • 给个免费网站好人有好报惠阳网站制作公司
  • 大型门户网站建设需要哪些技术泰安集团网站建设费用
  • 盐城网站开发代理咨询创客贴官网
  • 怎么做阿里巴巴国际网站首页导购类网站模板
  • 建设网站需要做的工作html代码编写
  • 网站建设相关新闻聊城制作手机网站公司
  • dw做网站怎么让文字移动制作网页和网站的区别
  • 获取网站访客qq代码手机上做app的软件
  • 天天向上 网站建设2017手机网站建设方案
  • 网站域名怎么设置郴州市建设局网站
  • 做环卫设备都有哪些网站论坛网站制作费用
  • 射阳网站建设郴州seo网络优化
  • 上海网站开发工程师招聘网企业做增资 网站平台
  • 广州做网站多少钱怎样进网站ftp
  • 找企业网站制作建设银行的积分网站
  • 家具行业做那个国际网站比较好win7 iis建立网站
  • 提高网站用户体验个人网站备案描述
  • 企业网站建设验收介休做网站
  • 地方网站盈利自己注册一个网站要多少钱
  • 公司注册网站多少钱如何查看网站的空间商
  • 商务网站建设的基本流程网络营销是一种什么样的营销
  • iis 网站绑定域名耒阳市做网站的
  • 公众号自己做电影网站人力资源网站
  • 可以直接进入网站的正能量网站建设一般多少个板块
  • 乐山网站开发网站seo是什么意思