C++ 函数声明末尾的“const”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3141087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is meant with "const" at end of function declaration?
提问by aPoC
I got a book, where there is written something like:
我有一本书,上面写着:
class Foo
{
public:
int Bar(int random_arg) const
{
// code
}
};
What does it mean?
这是什么意思?
回答by Janick Bernet
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 okay inside of the function, but writing inside of this function will generate a compiler error.
const
在函数声明后用关键字表示的“const 函数”使此类函数更改类的成员变量成为编译器错误。但是,在函数内部读取类变量是可以的,但是在该函数内部写入会产生编译器错误。
Another way of thinking about such "const function" is by viewing a 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.
考虑这种“const 函数”的另一种方式是将类函数视为采用隐式this
指针的普通函数。所以一个方法int Foo::Bar(int random_arg)
(最后没有 const )会产生一个像 的函数int Foo_Bar(Foo* this, int random_arg)
,而像这样的调用Foo f; f.Bar(4)
将在内部对应于像Foo f; Foo_Bar(&f, 4)
. 现在在末尾添加 const ( int Foo::Bar(int random_arg) const
) 然后可以理解为带有 const this 指针的声明:int Foo_Bar(const Foo* this, int random_arg)
。由于this
在这种情况下的类型是 const,因此不能修改成员变量。
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)
可以放松不允许函数写入类的任何变量的“const 函数”限制。即使函数被标记为“const 函数”,为了允许某些变量可写,这些类变量用关键字 标记mutable
。因此,如果一个类变量被标记为可变的,并且一个“const 函数”写入这个变量,那么代码将被干净地编译并且该变量是可能改变的。(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 applies when adding const
to the end of the function declaration after the parenthesis.
像往常一样,在处理const
关键字时,在 C++ 语句中改变 const 关键字的位置具有完全不同的含义。上述用法const
仅适用于添加const
到括号后的函数声明末尾。
const
is a highly overused qualifier in C++: the syntax and ordering is often not straightforward in combination with pointers. Some readings about const
correctness and the const
keyword:
const
是 C++ 中过度使用的限定符:语法和顺序与指针结合起来通常并不简单。关于const
正确性和const
关键字的一些阅读:
回答by Nikolai Fetissov
Consider two class-typed variables:
考虑两个类类型变量:
class Boo { ... };
Boo b0; // mutable object
const Boo b1; // non-mutable object
Now you are able to call anymember function of Boo
on b0
, but only const
-qualified member functions on b1
.
现在您可以调用on 的任何成员函数,但只能调用 on 限定的成员函数。Boo
b0
const
b1
回答by mkluwe
Bar
is guaranteed not to change the object it is being invoked on. See the section about const correctnessin the C++ FAQ, for example.
Bar
保证不会更改正在调用它的对象。例如,请参阅C++ FAQ 中关于 const 正确性的部分。
回答by JLWarlow
回答by Goz
I always find it conceptually easier to think of that you are making the this pointer const (which is pretty much what it does).
我总是发现在概念上更容易想到您正在使 this 指针成为 const (这几乎就是它所做的)。
回答by orbfish
Function can't change its parameters via the pointer/reference you gave it.
函数无法通过您提供的指针/引用更改其参数。
I go to this page every time I need to think about it:
每次我需要考虑的时候我都会去这个页面:
http://www.parashift.com/c++-faq-lite/const-correctness.html
http://www.parashift.com/c++-faq-lite/const-correctness.html
I believe there's also a good chapter in Meyers' "More Effective C++".
我相信 Meyers 的“更有效的 C++”中也有很好的一章。