本文翻译自:What is meant with “const” at end of function declaration? [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

  • Meaning of 'const' last in a function declaration of a class? 类的函数声明中“ const”最后的含义? 9 answers 9个答案

I got a book, where there is written something like: 我有一本书,上面写着这样的东西:

class Foo
{
public:int Bar(int random_arg) const{// code}
};

What does it mean? 这是什么意思?


#1楼

参考:https://stackoom.com/question/DB8h/函数声明末尾的-const-是什么意思-重复


#2楼

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. 在函数声明后用关键字const表示的“ const函数”使该类函数更改类的成员变量成为编译器错误。 However, reading of a class variables is okay 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 a class function as a normal function taking an implicit this pointer. 考虑这种“ const函数”的另一种方法是将类函数视为使用隐式this指针的普通函数。 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) 因此,方法int Foo::Bar(int random_arg) (末尾没有const)会产生类似int Foo_Bar(Foo* this, int random_arg)类的函数,并产生诸如Foo f; f.Bar(4)类的调用Foo f; f.Bar(4) Foo f; f.Bar(4) will internally correspond to something like Foo f; Foo_Bar(&f, 4) Foo f; f.Bar(4)在内部对应于Foo f; Foo_Bar(&f, 4) Foo f; Foo_Bar(&f, 4) . 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) . 现在在末尾添加const( int Foo::Bar(int random_arg) const )可以理解为带有const this指针的声明: 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. 由于类型this在这样的情况下是常数,没有成员变量的修改是可能的。

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 . 为了使某些变量即使在函数被标记为“ const函数”时也可写,这些类变量被标记为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. 因此,如果将类变量标记为可变的,并且“ const函数”写入此变量,则代码将干净地编译,并且可以更改该变量。 (C++11) (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. 通常,在处理const关键字时,更改C ++语句中const关键字的位置具有完全不同的含义。 The above usage of const only applies when adding const to the end of the function declaration after the parenthesis. const的上述用法仅适用于在括号后的函数声明末尾添加const的情况。

const is a highly overused qualifier in C++: the syntax and ordering is often not straightforward in combination with pointers. const是C ++中一个过度使用的限定符:与指针结合使用时,语法和顺序通常并不简单。 Some readings about const correctness and the const keyword: 有关const正确性和const关键字的一些读物:

Const correctness const正确性

The C++ 'const' Declaration: Why & How C ++“ const”声明:为什么和如何


#3楼

Similar to this question. 与此问题类似。

In essence it means that the method Bar will not modify non mutable member variables of Foo . 它在本质上意味着该方法Bar不会修改非易变的成员变量Foo


#4楼

Bar is guaranteed not to change the object it is being invoked on. 保证Bar不会更改正在调用它的对象。 See the section about const correctness in the C++ FAQ, for example. 例如,请参阅C ++ FAQ中有关const正确性的部分 。


#5楼

我总是从概念上更容易想到,您正在使this指针为const(几乎就是它的作用)。


#6楼

Consider two class-typed variables: 考虑两个类类型的变量:

class Boo { ... };Boo b0;       // mutable object
const Boo b1; // non-mutable object

Now you are able to call any member function of Boo on b0 , but only const -qualified member functions on b1 . 现在,您可以在b0上调用Boo 任何成员函数,但是在b1上只能调用const限定的成员函数。

函数声明末尾的“ const”是什么意思? [重复]相关推荐

  1. 类中成员函数声明后面的const的含义

    这个const一般是对类中成员函数属性的声明,但这个声明怪怪的,只能放在函数声明的尾部,大概是因为其它地方都已经被占用了.这个声明表示这个函数不会修改类中的任何数据成员.如果在编写const成员函数时 ...

  2. c++ 函数声明后面加上 const 有什么作用

    C++在函数声明时,后面跟个const是限定函数类型为常成员函数, 常成员函数是指不能改变成员变量值的函数. 例如"double d() const;",其中的其中的"c ...

  3. 函数声明后面的const用法

    void function() const{} 通常我们会看到一些函数声明后面会跟着一个const,这个const是做什么的呢? 看一下下面的例子,就知道了.直接在编译前,就会提示下面的两个错误 // ...

  4. C++ 函数声明后面的const用法

    转载 http://www.cnblogs.com/xing901022/p/3413019.html void function() const{} 通常我们会看到一些函数声明后面会跟着一个cons ...

  5. C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解

  6. C++ 笔记(15)— 引用(声明引用、引用作为参数、引用作为函数返回值、const 用于引用)

    引用是变量的别名.也就是说,它是某个已存在变量的另一个名字.一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量. 1. 创建引用 要声明引用,可使用引用运算符 & ,如下面的 ...

  7. const在函数声明中的应用(转)

    1.在一个函数声明中,const 可以修饰函数的返回值,或某个参数:对于成员函数,还可以修饰是整个函数.有如下几种情况,以下会逐渐的说明用法: A& operator=(const A& ...

  8. C++: C++函数声明的时候后面加const

    C++: C++函数声明的时候后面加const 转自:http://blog.csdn.net/zhangss415/article/details/7998123 非静态成员函数后面加const(加 ...

  9. [OHIF-Viewers]医疗数字阅片-医学影像-中间插播一下-es6-使用const加箭头函数声明函数相对于function声明函数有什么好处?...

    [OHIF-Viewers]医疗数字阅片-医学影像-中间插播一下-es6-使用const加箭头函数声明函数相对于function声明函数有什么好处? 这个好多人都已经写过了,这里插播一下,在OHIF- ...

最新文章

  1. C. Three Parts of the Array(切割字符串)
  2. HDU - 4705 Y(树形dp)
  3. html checked属性值,HTML复选框的checked属性的值是多少?
  4. mysql-dj数据准备-创建班级表
  5. 《人工智能:一种现代的方法》笔记(一)
  6. SMT工艺培训一日谈
  7. 分享294个PHP博客系统,总有一款适合你
  8. 电磁场与电磁波-场论基础
  9. 计算机二级都有题库的吗,全国计算机二级考试的题库每年都变吗?我能不能拿去年的未来教育的题库来做,还会有原题吗?急...
  10. python怎么设置颜色深浅变化_【opencv_python学习之三】图像处理(一)更改色彩模式...
  11. 计算机如何连接wifi台式,无线网卡怎么连接台式电脑_台式机添加无线网的方法...
  12. 单号自动识别查询 支持一键复制导出
  13. 2020中国汽车后市场白皮书
  14. 图纸识别自动生成BOM清单的方法
  15. Mongo数据库简介
  16. 运维常说的 5个9、4个9、3个9 的可靠性,到底是什么鬼?
  17. 如何对PDF文档进行数字签名
  18. 360度全景图像制作
  19. 百度一键Root使用教程
  20. 贵州支教之第一天(11月7日)

热门文章

  1. 【Android】最近做的一个Android平台下时间统计工具
  2. java 反射详解通俗易懂
  3. java里css查找快捷键_CSS入门2—元素快捷键
  4. python伪造邮件发件地址_python写一个邮箱伪造脚本
  5. PHP学习笔记-字符串操作2
  6. Android 新手常见的10个误区(上)
  7. (0081)iOS开发之无限后台定位并上传数据到服务器
  8. 数字建模matlab,Matlab基础及数学建模.ppt
  9. Recyclerview设置间距
  10. 07-函数作用域和集合列表字典元祖