C++ 成员函数末尾的 const 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4059932/
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 the meaning of a const at end of a member function?
提问by Mat
What exactly does the const
keyword in C++ mean when it's written at the end of a member function (after the argument list)?
const
当 C++中的关键字写在成员函数的末尾(在参数列表之后)时,它究竟是什么意思?
回答by Oliver Charlesworth
It means that *this
is const
inside that member function, i.e. it doesn't alter the object.
这意味着,*this
是const
该成员函数的内部,即,它不改变的对象。
The keyword
this
is a prvalue expression whose value is the address of the object for which the function is called. The type ofthis
in a member function of a classX
isX*
. If the member function is declaredconst
, the type ofthis
isconst X*
. [section 9.3.2 §1]In a
const
member function, the object for which the function is called is accessed through aconst
access path; therefore, aconst
member function shall not modify the object and its non-static data members. [section 9.3.2 §2]
关键字
this
是一个纯右值表达式,其值是调用函数的对象的地址。类的this
成员函数中的类型X
是X*
。如果声明了成员函数const
,则其类型this
为const X*
。[第 9.3.2 节 §1]在
const
成员函数中,通过const
访问路径访问调用该函数的对象;因此,const
成员函数不应修改对象及其非静态数据成员。[第 9.3.2 节 §2]
This means that a const
member function can be called on a const
instance of the class. A non-const
member function can't be called on [1]a const
object, since it could potentially try to modify it.
这意味着const
可以const
在类的实例上调用成员函数。阿非const
成员函数不能被称为上[1]一个const
对象,因为它可能尝试修改它。
[1]Note: a temporary is not a const
object unless it's of const
type.
[1]注意:临时const
对象不是对象,除非它是const
类型。
回答by wilhelmtell
const
at the end of a function signature means that the function should assume the object of which it is a member is const
. In practical terms it means that you ask the compiler to checkthat the member function does not change the object data in any way. It means asking the compiler to check that it doesn't directly change any member data, and it doesn't call any function that itself does not guarantee that it won't change the object.
const
在函数签名的末尾意味着该函数应该假定它是其成员的对象 is const
。实际上,这意味着您要求编译器检查成员函数是否以任何方式更改对象数据。这意味着要求编译器检查它没有直接更改任何成员数据,并且它不会调用任何本身不能保证它不会更改对象的函数。
When you create a const
object you are asking the compiler to make sure that that object does not change beyond its initialization. That in turns means that the compiler will check you don't directly change its member data and that you don't call any function that does not guarantee it won't change the object.
当您创建一个const
对象时,您是在要求编译器确保该对象在初始化后不会更改。这反过来意味着编译器将检查您没有直接更改其成员数据,并且您没有调用任何不能保证它不会更改对象的函数。
This is all part of the const correctnessphilosophy. In essence it means that if things work right now and they won't change then they will never break. In other words, constant things are easier to work with reliably. This const
thing at the end of function signatures is a tool for you to prohibit things from breaking. This in turns means you should put const
everywhere you possibly can.
这都是const 正确性哲学的一部分。从本质上讲,这意味着如果事情现在正常工作并且它们不会改变,那么它们将永远不会破裂。换句话说,不变的东西更容易可靠地工作。const
函数签名末尾的这个东西是你禁止破坏东西的工具。这反过来意味着你应该把const
你可能的地方放在任何地方。
回答by Steve Townsend
Compiler optimizations are possible, but the main benefit is in enforcing the contract expressed in the function's declaration - if you define a member function as const
, the compiler prevents any modification to the object inside that function.
编译器优化是可能的,但主要好处是强制执行函数声明中表达的契约 - 如果将成员函数定义为const
,编译器会阻止对该函数内的对象进行任何修改。
You can exempt individual fields in the class from this restriction using mutable
in their declaration. This is useful for example when you have a class that encapsulates its own lock_guard, which must change its value to enforce thread safety even within const
member functions.
您可以mutable
在声明中使用该类中的单个字段免除此限制。例如,当您有一个封装了自己的 lock_guard 的类时,这很有用,即使在const
成员函数中,它也必须更改其值以强制执行线程安全。