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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:25:02  来源:igfitidea点击:

What is the meaning of a const at end of a member function?

c++const

提问by Mat

What exactly does the constkeyword 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 *thisis constinside that member function, i.e. it doesn't alter the object.

这意味着,*thisconst该成员函数的内部,即,它不改变的对象。

The keyword thisis a prvalue expression whose value is the address of the object for which the function is called. The type of thisin a member function of a class Xis X*. If the member function is declared const, the type of thisis const X*. [section 9.3.2 §1]

In a constmember function, the object for which the function is called is accessed through a constaccess path; therefore, a constmember function shall not modify the object and its non-static data members. [section 9.3.2 §2]

关键字this是一个纯右值表达式,其值是调用函数的对象的地址。类的this成员函数中的类型XX*。如果声明了成员函数const,则其类型thisconst X*。[第 9.3.2 节 §1]

const成员函数中,通过const访问路径访问调用该函数的对象;因此,const成员函数不应修改对象及其非静态数据成员。[第 9.3.2 节 §2]

This means that a constmember function can be called on a constinstance of the class. A non-constmember function can't be called on [1]a constobject, since it could potentially try to modify it.

这意味着const可以const在类的实例上调用成员函数。阿非const成员函数不能被称为上[1]一个const对象,因为它可能尝试修改它。

[1]Note: a temporary is not a constobject unless it's of consttype.

[1]注意:临时const对象不是对象,除非它是const类型。

回答by wilhelmtell

constat 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 constobject 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 constthing at the end of function signatures is a tool for you to prohibit things from breaking. This in turns means you should put consteverywhere 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 mutablein 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 constmember functions.

您可以mutable在声明中使用该类中的单个字段免除此限制。例如,当您有一个封装了自己的 lock_guard 的类时,这很有用,即使在const成员函数中,它也必须更改其值以强制执行线程安全。