“const”是什么意思?在函数声明的末尾?
我有一本书,上面写着这样的话:
class Foo
{
public:
int Bar(int random_arg) const
{
// code
}
};
这是什么意思?
I got a book, where there is written something like:
class Foo
{
public:
int Bar(int random_arg) const
{
// code
}
};
What does it mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
“const 函数”(在函数声明后用关键字 const 表示)会使此类函数更改类的数据成员时出现编译器错误。但是,在函数内部读取类变量是可以的,但在该函数内部写入会产生编译器错误。
思考此类“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,因此无法修改数据成员。可以放宽不允许函数写入类的任何变量的“const 函数”限制。为了允许某些变量即使在函数被标记为“const 函数”时也可写,这些类变量用关键字
mutable
进行标记。因此,如果一个类变量被标记为可变的,并且“const 函数”写入该变量,那么代码将干净地编译并且该变量可以更改。 (C++11)与往常一样,处理
const
关键字时,更改 C++ 语句中 const 关键字的位置具有完全不同的含义。上述const
的用法仅适用于将const
添加到函数声明末尾括号后的情况。const
是 C++ 中过度使用的限定符:与指针结合使用时,语法和排序通常并不简单。有关const
正确性和const
关键字的一些读物:Const 正确性
C++ 'const' 声明:为什么 &怎样做
A "const function", denoted with the keyword
const
after a function declaration, makes it a compiler error for this class function to change a data member 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.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 methodint Foo::Bar(int random_arg)
(without the const at the end) results in a function likeint Foo_Bar(Foo* this, int random_arg)
, and a call such asFoo f; f.Bar(4)
will internally correspond to something likeFoo 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 ofthis
in such case is const, no modifications of data members 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 ofconst
only applies when addingconst
to the end of the function declaration after the parenthesis.const
is a highly overused qualifier in C++: the syntax and ordering is often not straightforward in combination with pointers. Some readings aboutconst
correctness and theconst
keyword:Const correctness
The C++ 'const' Declaration: Why & How
考虑两个类类型变量:
现在您可以在
b0
上调用Boo
的任何成员函数,但只能调用constb1
上的 code> 限定成员函数。Consider two class-typed variables:
Now you are able to call any member function of
Boo
onb0
, but onlyconst
-qualified member functions onb1
.Bar
保证不会更改正在调用它的对象。例如,请参阅 C++ 常见问题解答中的有关 const 正确性的部分 。Bar
is guaranteed not to change the object it is being invoked on. See the section about const correctness in the C++ FAQ, for example.类似于这个问题。
本质上,这意味着方法
Bar
不会修改非Foo
的可变成员变量。Similar to this question.
In essence it means that the method
Bar
will not modify non mutable member variables ofFoo
.我总是发现从概念上更容易想到你正在将 this 指针设置为常量(这几乎就是它的作用)。
I always find it conceptually easier to think of that you are making the this pointer const (which is pretty much what it does).
函数无法通过您提供的指针/引用更改其参数。
每次我需要考虑它时,我都会转到此页面:
http:// /www.parashift.com/c++-faq-lite/const- Correctness.html
我相信 Meyers 的“更有效的 C++”中也有一个很好的章节。
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
I believe there's also a good chapter in Meyers' "More Effective C++".