函数尾部的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?的更多相关文章

  1. 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 ...

  2. 类成员函数后边加const

    本文主要整理自stackoverflow上的一个对问题Meaning of “const” last in a C++ method declaration?的回答. 测试1 对于下边的程序,关键字c ...

  3. 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 ...

  4. [C++]const修饰符

    Date: 2014-1-1 Summary: const 修饰符笔记 Contents: 1.const 修饰符 声明一个常量数据类型 , 在编译时就确定数据类型 2.const 与 指针 一般情况 ...

  5. 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 ...

  6. Dalvik虚拟机java方法执行流程和Method结构体分析

    Method结构体是啥? 在Dalvik虚拟机内部,每个Java方法都有一个对应的Method结构体,虚拟机根据此结构体获取方法的所有信息. Method结构体是怎样定义的? 此结构体在不同的andr ...

  7. C# 类 (11) - Const

    Const variable 变量 ,值可变的constant 常量,不可变,C# 里关键字是const当我们定义一个常量的时候,需要立马赋值,以后不能再改这个量了我们可以把常量定义在 method ...

  8. Effective Java 62 Document all exceptions thrown by each method

    Principle Always declare checked exceptions individually, and document precisely the conditions unde ...

  9. @Override must override a superclass method 问题解决

    一.问题的由来 最近接手了了一个合作企业的项目,前期不是我司开发的,上周做了几天的技术对接,客户端界面由我负责对接,从svn检出之后,迫不及待的导入到了本地的myeclipse中,谁知立马就出现了那个 ...

随机推荐

  1. 在windows下编译ffmpeg

    编译ffmpeg,我在网上找了很多相关的方法,但最后都没编译成功. 所以下面就记录下自己的编译方法吧,留着以后编译的时候做参考. 1.首先,下载编译工具MinGW+Msys,搭建编译环境.工具下载地址 ...

  2. CentOS基础命令大全

    1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours: ...

  3. optimization blocks (csapp chapter 5.1)

    p_511 编译器在没有指示下,会做‘ safe optimization',因此有些优化在没有参数的指示下是不会对代码做优化的,故在程序中应该避免某一类代码,因为它们妨碍了编译器做优化. optim ...

  4. 在程序中使用命令行的方式来调用py文件

    做这个主要是程序可以做到直接调用一个脚本,而不是从脚本中把类或者函数import出来这样调用,比如我们写的python命令行文件,让java来调用,让c++来调用,都是可以的.这样不需要整个语言都用p ...

  5. python--条件判断和循环--3

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 一.if语句 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄, ...

  6. ASP.Net MVC开发基础学习笔记(7):数据查询页面

     前言 前面铺垫了那么多,今天我们就用MVC5 + EF6 + Bootstrap3来正式创建一个基本查询页面. 为什么从查询页面開始?嘿嘿.由于小弟的.Net生涯就是从查询页面開始的,记得正式工 ...

  7. iOS根据文字字数动态确定Label宽高

    我们有时候在写项目的时候,会碰到,意见反馈,还有其他地方,讲座活动细则等需要大篇展示的文本, 因为每次服务器返回的内容大小不一,所以需要动态的调整label的宽高: 在ios 6 的时候可以: -(v ...

  8. YARN应用程序开发和设计流程

    从业务的角度看,一个应用需要分两部分进行开发,一个是接入YARN平台,实现3个协议,通过YARN实现对集群资源的访问和利用:另一个是业务功能的实现,这个与YARN本身没有太大关系.下面主要阐述如何将一 ...

  9. DOCKER在windows上安装与配置

    1.下载程序 安装包 https://github.com/boot2docker/windows-installer/releases(这个地址国内下载很慢) 用这个: https://get.da ...

  10. 让nginx支持.htaccess文件实现伪静态的方法!

    搞了个VPS,装了个LNMP一键安装包,然后搞了个程序放上,想实现伪静态,但是lnmp的默认规则里没有河蟹男需要的伪静态文件,得自己配置, 以前只在.htaccess里搞下就行,现在换Linux下ng ...