文档章节

C++之 const

SVD
 SVD
发布于 2015/11/07 16:10
字数 412
阅读 65
收藏 0

In the C, C++, and D programming languages, const is a type qualifier, a keyword applied to a data type that indicates that the data is constant (does not vary). While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in being part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking.

const was introduced by Bjarne Stroustrup in C with Classes, the predecessor to C++, in 1981, and was originally called readonly.As to motivation, Stroustrup writes:

"It served two functions: as a way of defining a symbolic constant that obeys scope and type rules (that is, without using a macro) and as a way of deeming an object in memory immutable."

The first use, as a scoped and typed alternative to macros, was analogously fulfilled for function-like macros via the inline keyword. Constant pointers, and the * const notation, were suggested by Dennis Ritchie and so adopted.

const was then adopted in C as part of standardization, and appears in C89 (and subsequent versions) along with the other type qualifier, volatile. A further qualifier, noalias, was suggested at the December 1987 meeting of the X3J11 committee, but was rejected; its goal was ultimately fulfilled by the restrict keyword in C99. Ritchie was not very supportive of these additions, arguing that they did not "carry their weight", but ultimately did not argue for their removal from the standard.

D subsequently inherited const from C++, where it is known as a type constructor (not type qualifier) and added two further type constructors, immutable and inout, to handle related use cases.

#include <iostream>
#include <stdlib.h>
using namespace std;

void fun(const int &a, const int &b);
int main(void)
{
	int x = 3;
	int y = 5;
	
	fun(x, y);
	cout << x << "," << y << endl;
	system("pause");
	return 0;

}
void fun(const int &a, const int &b)
{
	a = 10;
	b = 20;

}

以上的程序之所以运行时报错,就是因为const关键字的限定,使得函数的形参无法改变实参的原来的值,从而在其他的程序设计中避免了误操作的发生。在本例中a,b的值无法发生改变,程序报错。

© 著作权归作者所有

共有 人打赏支持
上一篇: C++之函数重载
下一篇: C++之引用
SVD

SVD

粉丝 34
博文 207
码字总数 102940
作品 0
海淀
私信 提问
const static在c c++中的作用

一.C语言中的const const是C语言中保留的一个关键字,它用来限定一个变量是只读的,即不可变的。程序中使用const可以在一定程度上提高程序的健壮性,但是程序中使用过多的const,可能在对代码...

y_x
2011/11/17
0
0
MySQL Connector/C++ 1.1.4 发布

MySQL Connector/ODBC 是 MySQL 数据库的官方C++驱动程序。 MySQL Connector/C++ 1.1.4 发布,此版本的二进制版本需要使用 Boost 1.54.0 编译。 新增的特性和改进 Connector/C++ 支持以下的连...

oschina
2014/08/01
3.6K
5
const与#define

C++中const符号表原理图 const int a=10; 当遇见常量声明时,在符号表中放入常量 编译过程中若发现使用常量则直接以符号表中的值替换 编译过程中若发现对const使用了entern或者&操作符,则给...

lybnt
2018/02/19
3
1
利用 C++ Interop 封装 ISO C++ 对象, 供其他 .Net 语言使用

.Net BCL 支持两种互操作技术,模块 级重用 P/Invoke 和组件级重用COM 互操作, C++/CLI 除了支持以上两种互操作以外,更支持代码级重用, 称为 C++ Interop,本文 仅举例说明这种互操作技术。...

Yong_Luo
2010/10/01
0
0
SonarQube C/C++/Objective-C 4.2 发布

SonarQube C/C++/Objective-C 4.2 发布了,这是一个C/C++/Objective-C的插件,用于检查代码质量。 这个版本提供了以下的新规则: “nonnull” pointers should not be set to null (C, Bug) ...

局长
2016/10/06
1K
0

没有更多内容

加载失败,请刷新页面

加载更多

mysql 系统设置SQL

打开、关闭日志 SET GLOBAL general_log = 'Off'; SET GLOBAL general_log = 'On'; 查看日志是否打开 show variables like '%general%';...

jingshishengxu
今天
2
0
转行学大数据,如何选择如何学习大数据开发?

大数据火了几年了,但是今年好像进入了全民大数据时代,本着对科学的钻(zhun)研(bei)精(tiao)神(cao),我在17年年初开始自学大数据,后经过系统全面学习,于这个月跳槽到现任公司。 现在已经...

董黎明
今天
4
0
RadosClient OSDC

RadosClient.h class librados::RadosClient : public Dispatcher//继承自Dispatcher(消息分发类){public: using Dispatcher::cct; md_config_t *conf;//配置文件private: ......

banwh
今天
3
0
如果让你写一个消息队列,该如何进行架构设计?

面试题 如果让你写一个消息队列,该如何进行架构设计?说一下你的思路。 面试官心理分析 其实聊到这个问题,一般面试官要考察两块: 你有没有对某一个消息队列做过较为深入的原理的了解,或者...

李红欧巴
今天
6
0
错题

无知的小狼
今天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部