Meaning of “const” last in a C++ method declaration?
函数尾部的const是什么意思?
1 Answer by Jnick Bernnet
A "const function", denoted with the keyword const after a function declaration, makes it a compiler error for this class function to change a member variable of the class. However, reading of a class variables is ok inside of the function, but writing inside of this function will generate a compiler error.
Another way of thinking about such "const function" is by viewing an class function as a normal function taking an implicit this pointer. So a method int Foo::Bar(int random_arg) (without the const at the end) results in a function like int Foo_Bar(Foo* this, int random_arg), and a call such as Foo f; f.Bar(4) will internally correspond to something like Foo f; Foo_Bar(&f, 4). Now adding the const at the end (int Foo::Bar(int random_arg) const) can then be understood as a declaration with a const this pointer: int Foo_Bar(const Foo* this, int random_arg). Since the type of this in such case is const, no modifications of member variables are possible.
It is possible to loosen the "const function" restriction of not allowing the function to write to any variable of a class. To allow some of the variables to be writable even when the function is marked as a "const function", these class variables are marked with the keyword mutable. Thus, if a class variable is marked as mutable, and a "const function" writes to this variable then the code will compile cleanly and the variable is possible to change. (C++11)
As usual when dealing with the const keyword, changing the location of the const key word in a C++ statement has entirely different meanings. The above usage of const only apply when adding const to the end of the function declaration after the parenthesis. const is a highly overused qualifier in C++ and the syntax and ordering is often not straightforward in combination with pointers. Some readings about const correctness and the const keyword:
Meaning of “const” last in a C++ method declaration?的更多相关文章
- Natural language style method declaration and usages in programming languages
More descriptive way to declare and use a method in programming languages At present, in most progra ...
- 类成员函数后边加const
本文主要整理自stackoverflow上的一个对问题Meaning of “const” last in a C++ method declaration?的回答. 测试1 对于下边的程序,关键字c ...
- Modified Least Square Method and Ransan Method to Fit Circle from Data
In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn't provide function to f ...
- [C++]const修饰符
Date: 2014-1-1 Summary: const 修饰符笔记 Contents: 1.const 修饰符 声明一个常量数据类型 , 在编译时就确定数据类型 2.const 与 指针 一般情况 ...
- Just4Fun - Comparaison between const and readonly in C#
/* By Dylan SUN */ Today let us talk about const and readonly. const is considered as compile-time c ...
- Dalvik虚拟机java方法执行流程和Method结构体分析
Method结构体是啥? 在Dalvik虚拟机内部,每个Java方法都有一个对应的Method结构体,虚拟机根据此结构体获取方法的所有信息. Method结构体是怎样定义的? 此结构体在不同的andr ...
- C# 类 (11) - Const
Const variable 变量 ,值可变的constant 常量,不可变,C# 里关键字是const当我们定义一个常量的时候,需要立马赋值,以后不能再改这个量了我们可以把常量定义在 method ...
- Effective Java 62 Document all exceptions thrown by each method
Principle Always declare checked exceptions individually, and document precisely the conditions unde ...
- @Override must override a superclass method 问题解决
一.问题的由来 最近接手了了一个合作企业的项目,前期不是我司开发的,上周做了几天的技术对接,客户端界面由我负责对接,从svn检出之后,迫不及待的导入到了本地的myeclipse中,谁知立马就出现了那个 ...
随机推荐
- Zookeeper 工作流
一旦ZooKeeper集合启动,它将等待客户端连接.客户端将连接到ZooKeeper集合中的一个节点.它可以是leader或follower节点.一旦客户端被连接,节点将向特定客户端分配会话ID并向该 ...
- URI、URL和URN之间的区别
URI标识一个事物,URL定位一个事物:然而,位置同样可以标识一个事物,所以,每个URL都是一个 URI,但一个URI并不一定是一个URL. 举例说明 罗杰·佩特 这是我的名字,这是一个标识.它就像一 ...
- 关于对afx_msg的解释-----来源百度百科
1AFX前缀 Afx前缀是微软MFC一个小组的名称简写,并没有别的意义. MFC的很多代码,包括全局函数名.宏.头文件名都使用了"Afx". Afx*.h是一组MFC的核心头文件, ...
- kafka学习之-文件存储机制
Kafka是什么 Kafka是最初由Linkedin公司开发,是一个分布式.分区的.多副本的.多订阅者,基于zookeeper协调的分布式日志系统(也可以当做MQ系统),常见可以用于web/nginx ...
- [转]查看处于被锁状态的表:v$locked_object dba_objects v$session all_objects v$sqlarea v$lock
oracle官网当一个用户发出select..for update的错作准备对返回的结果集进行修改时,如果结果集已经被另一个会话锁定,就是发生阻塞.需要等另一个会话结束之后才可继续执行.可以通过发出 ...
- Socket.BeginConnect 方法
Socket.BeginConnect 方法 (IPAddress, Int32, AsyncCallback, Object) 开始一个对远程主机连接的异步请求. 主机由 IPAddress 和端口 ...
- error:2014 Commands out of sync; you can't run this command now
如下错误: 分析原因: 前端ajax请求后台,共用同一个链接. 搜索别人的解决方案:http://blog.csdn.net/grass_ring/article/details/3499402 用m ...
- js 去掉空格.回车.换行
Jquery:$("#accuracy").val($("#accuracy").val().replace(/\ +/g,""));//去 ...
- 在懂得BREW接口的原理之后, 那么该知道BREW接口是如何声明和实现了
参考:http://blog.csdn.net/peteryxk/article/details/1584514 首先介绍几个用到的宏定义: l #define VTBL(iname) ...
- ios 在https情况下,使用webview加载url出错的解决方法 ios9 适配问题
修改info.plist文件,添加App Transport Security Settings,然后在这个里面添加Allow Arbitrary Loads,改为yes 如下图: