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中,谁知立马就出现了那个 ...
随机推荐
- Java JNI初探
---说明,之前直接百度出来的例子,照猫画虎.没想到的是这例子居然直接来自百度百科,写着写着就囧了.. ---anyway,写完了就当是给自己看吧. 同事求助,就看了一下,照猫画虎一番,略有所得. J ...
- web 开发之酷炫--- 酷炫展示
http://www.cnblogs.com/dsxniubility/p/4588560.html
- Spring-JDBC配置
以C3P0连接池为例:由于C3P0是第三方,我们无法使用注解将其定义为bean,因此需要在applicationContext.xml中配置: <!-- 导入配置文件 --> <co ...
- 4 kafka集群部署及kafka生产者java客户端编程 + kafka消费者java客户端编程
本博文的主要内容有 kafka的单机模式部署 kafka的分布式模式部署 生产者java客户端编程 消费者java客户端编程 运行kafka ,需要依赖 zookeeper,你可以使用已有的 zo ...
- 调用外部 DLL 中的函数(2. 晚绑定)
, b, t, );end; procedure TForm1.FormDestroy(Sender: TObject);begin FreeLibrary(inst); {记得释放}end; e ...
- mysql数据库不能远程访问的问题
1.先暂停防火墙,检查是不是防火墙的问题. 2. 如若不是防火墙的问题,则可能是用户权限的问题. 这里创建一个用户来用于远程连接:首先登陆你的mysql数据库 命令: mysql -uroot -p ...
- 各大IT公司 技术博客汇总
来自:http://www.cnblogs.com/IT-Bear/p/3191423.html 腾讯系列(13) 阿里系列(18) 百度系列(3) 搜狐系列(3) 新浪系列(2) 360系 ...
- MathType编辑指数的方法
利用MathType编辑公式使得在文档中编辑理工类的论文工作减轻了不少,它所包含的符号与模板基本都可以满足我们日常工作学习中对公式的需要.在文档中编辑数学物理符号或者是函数表达式,都是用word公式编 ...
- linux ,cron定时任务 备份mysql数据库
cron 定时任务执行备份脚本文件 backup.sh #!/bin/bash USER="root" PASSWORD="xxxxx" DATABASE=&q ...
- Access数据操作-02
数据库连接 MDB文件 :Provider=Microsoft.Jet.OLEDB.4.0;Data Source=*.mdb ;Persist Security Info=False; AccDB文 ...