博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
函数声明末尾的“ const”是什么意思? [重复]
阅读量:2290 次
发布时间:2019-05-09

本文共 3610 字,大约阅读时间需要 12 分钟。

本文翻译自:

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

  • 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楼

参考:


#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关键字的一些读物:


#3楼

Similar to question. 与问题类似。

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


#4楼

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


#5楼

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


#6楼

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

class Boo { ... };Boo b0;       // mutable objectconst 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限定的成员函数。

转载地址:http://escnb.baihongyu.com/

你可能感兴趣的文章
UILabel常用属性详解
查看>>
UITextField常用属性和方法详解
查看>>
“UITableView完美平滑滚动”阅读笔记
查看>>
UIImageView常用属性和方法
查看>>
UIImage常用属性和方法
查看>>
会报编译器警告的Xcode 6.3新特性:Nullability Annotations
查看>>
2015 Objective-C 三大新特性
查看>>
Objective-C中instancetype详解
查看>>
音频、视频框架概括说明
查看>>
手势(UIGestureXXX)使用详解
查看>>
UIMenuController和UIMenuItem,即iOS剪贴板
查看>>
新一代数据查询语言GraphQL来啦
查看>>
Simple Zend_Layout Example
查看>>
The Zend Framework MVC Architecture
查看>>
Framework框架分析总结
查看>>
Windows7下centOS 硬盘安装双系统
查看>>
GRUB引导程序参数
查看>>
phpMyAdmin简明安装教程
查看>>
独立安装LAMP时需要注意的几点
查看>>
socket
查看>>