[英]Simple question about C++ constant syntax
Here is some code copied from Thinking in C++ Vol1 Chapter 10. 这里有一些代码复制自Thinking in C ++ Vol1第10章。
#include <iostream>
using namespace std;
int x = 100;
class WithStatic {
static int x;
static int y;
public:
void print() const {
cout << "WithStatic::x = " << x << endl;
cout << "WithStatic::y = " << y << endl;
}
};
what's the meaning of const for the function print()? const函数print()的含义是什么? Thanks!
谢谢!
I've heard this described previously as “a method that does not logically change the object”. 我之前听说这是“一种不会在逻辑上改变对象的方法”。 It means that by calling this method the caller can expect the object's state to remain the same after the method returns.
这意味着通过调用此方法,调用方可以在方法返回后期望对象的状态保持不变。 Effectively, the
this
pointer becomes a constant pointer to a constant instance of that class, so member variables cannot be altered. 实际上,
this
指针成为指向该类的常量实例的常量指针,因此不能更改成员变量。 The exception to this rule is if member variables are declared with mutable
. 此规则的例外是成员变量是否声明为
mutable
。 If a class has mutable
member variables, these can be modified by both non-const and const methods. 如果类具有
mutable
成员变量,则可以通过非const和const方法修改这些变量。 Also, non-const methods cannot be called from within a const method. 此外,不能从const方法中调用非const方法。
Some people use mutable
member variables to cache results of timely computations. 有些人使用
mutable
成员变量来缓存及时计算的结果。 In theory, the state of the object does not change (ie the only effect is that subsequent calls are quicker, but they produce the same results given the same input). 理论上,对象的状态不会改变(即唯一的影响是后续调用更快,但是在给定相同输入的情况下它们会产生相同的结果)。
It means that it doesn't change any member variables of the class. 这意味着它不会更改类的任何成员变量。
http://www.parashift.com/c++-faq-lite/const-correctness.html http://www.parashift.com/c++-faq-lite/const-correctness.html
It's a guarantee that the function won't modify the object at all (ie it's a "read-only" function). 它保证函数根本不会修改对象(即它是“只读”函数)。 EDIT: Apparently, the exception to that rule is if an object has
mutable
members; 编辑:显然,该规则的例外是对象是否具有
mutable
成员; those can be changed by both const and non-const functions. 那些可以通过const和非const函数来改变。
Also, I'm glad to see somebody else learning from TIC++. 另外,我很高兴看到其他人从TIC ++学习。 It's a great resource for beginners.
这对初学者来说是一个很好的资源。
What it does is effectively make the this
pointer be a const pointer to const instead of a const pointer to non-const. 它的作用是有效地使
this
指针成为const的const指针,而不是指向非const的const指针。 So, any time that you reference this
in a const member function - either explicitly or implicitly - you're using a const pointer to const. 因此,任何时候你在const成员函数中引用
this
- 显式或隐式 - 你使用const指针指向const。
So, in the case of the class you have here, in any non-const function, the type of this
is WithStatic const *
while in the const functions, its type is const WithStatic * const
. 所以,在你这里的类,在任何非const的功能的情况下,类型
this
是WithStatic const *
而在const函数,它的类型是const WithStatic * const
。
Like with any pointer to const, you can't alter anything that it points to. 就像任何指向const的指针一样,你不能改变它所指向的任何东西。 So, you can't alter any of its member variables, and you can't call any of its non-const member functions.
因此,您不能更改其任何成员变量,也不能调用其任何非const成员函数。
Generally speaking, it's a good idea to make a member function be const if you can reasonably do so, because it guarantees that you're not going to alter the object's state, and you can call it with a const object. 一般来说,如果你可以合理地将成员函数设为const是个好主意,因为它保证你不会改变对象的状态,你可以用const对象调用它。
It is possible for member variables to be altered if they're mutable
or volatile
, but those are more advanced topics that are probably better avoided until you're more familiar with the language. 这是可能的,如果他们的成员变量被改变
mutable
或volatile
,但这些都是可能最好避免,直到你更熟悉的语言更高级的主题。 Certainly, you don't usually need to worry about them and shouldn't use them unless you need to. 当然,你通常不需要担心它们,除非你需要,否则不应该使用它们。 It's also possible to cast away the constness of the
this
pointer, at which point you could alter it, but IIRC, that's undefined behavior, and it's definitely generally considered a bad idea. 也可以抛弃
this
指针的常量,此时你可以改变它,但IIRC,那是未定义的行为,而且它通常被认为是一个坏主意。 So, there are instances where it's possible to alter an object's state within a const member function, but it's not normally possible and best avoided even when it is. 因此,有些情况下可以在const成员函数中改变对象的状态,但通常不可能并且即使它是最好的也可以避免。
When you make a member function const, you're effectively promising that the object's state will not be changed by that function call (though there can obviously be side effects as evidenced by the fact that you can call functions like printf()
). 当你创建一个成员函数const时,你有效地承诺该函数调用不会改变对象的状态(尽管显然可能有副作用,因为你可以调用
printf()
类的printf()
)。
If a member function is declared "const", it means that the function will not modify the object's state. 如果成员函数声明为“const”,则表示该函数不会修改对象的状态。 This:
这个:
For all the above reasons, one should, as a general rule of thumb, declare a function as "const" if it does not logically modify the state of the object. 由于上述所有原因,作为一般经验法则,如果它不逻辑地修改对象的状态,则应该将函数声明为“const”。 If the logical state of the object changes, then don't use "const".
如果对象的逻辑状态发生变化,则不要使用“const”。 As an aside, there are cases where the actual state changes but the logical state does not (eg caching), in which case one should still mark the function as "const", but one needs to use the "mutable" keyword with the caching variables in order to tell the compiler that modifying them actually doesn't change the logical state.
顺便说一下,有些情况下实际状态会发生变化,但逻辑状态却没有(例如缓存),在这种情况下,仍然应该将函数标记为“const”,但是需要在缓存中使用“mutable”关键字变量,以告诉编译器修改它们实际上不会改变逻辑状态。
Intuitively, it means that the method doesn't modify the object on which it is called. 直观地说,这意味着该方法不会修改调用它的对象。 But really, it means two things:
但实际上,这意味着两件事:
const
. const
的对象或对象指针上调用该方法。 this
pointer becomes const
. this
指针变为const
。 Consequently, all members (except those declared mutable
) are const
, ie read-only, within the context of the method. mutable
成员除外)在方法的上下文中是const
,即只读。 However, it may still modify: this
pointer — including global data, data pointed to by the method's parameters, static members of the class, other objects of the class, even the same object (if it happens to have access to a non-const pointer to it or bypasses constancy using a const_cast
). this
指针访问的数据 - 包括全局数据,方法参数指向的数据,类的静态成员,类的其他对象,甚至同一个对象(如果碰巧有访问非const指针的话)使用const_cast
来绕过或绕过constancy。 const表示不允许print()修改未标记为mutable的状态变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.