在成员函数结束时const的含义是什么? [英] What is the meaning of a const at end of a member function?
问题描述
C ++中的 const
关键字是什么意思,当它写在成员函数的结尾(在参数列表之后)?
What exactly does the const
keyword in C++ mean when it's written at the end of a member function (after the argument list)?
推荐答案
这意味着 * this
是 const
关键字
this
是一个prvalue表达式,其值是调用函数的对象的地址。类X
的成员函数中的类型
X
c $ c>。如果成员函数声明为const
,则this
的类型为const X *
。 [section 9.3.2§1]
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]
在一个 const
成员函数中,函数被调用的对象通过 const
访问路径访问;因此, const
成员函数不应修改对象及其非静态数据成员。 [section 9.3.2§2]
In a const
member function, the object for which the function is called is accessed through a const
access path; therefore, a const
member function shall not modify the object and its non-static data members. [section 9.3.2 §2]
这意味着 const
函数可以在类的 const
实例上调用。不能在 [1] a const
上调用非成员函数对象,因为它可能尝试修改它。
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.
[1] 注意: const
对象,除非它是 const
类型。
[1] Note: a temporary is not a const
object unless it's of const
type.
这篇关于在成员函数结束时const的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!