c++ 中 const 和没有 const 方法?
我有一个程序,它的许多类都有一些带有关键字 const 的运算符和方法,如下所示:
operator const char* () const;
operator char* ();
void Save(const char *name) const;
void Load(const char *name);
首先:方法声明末尾的 const 是什么意思?,它与放在开头一样吗?
第二:为什么需要 const 版本和非 const 版本的operator()?
提前致谢。
I have a program and many of its classes have some operators and methods with the keyword const like the followings:
operator const char* () const;
operator char* ();
void Save(const char *name) const;
void Load(const char *name);
First: what does it mean const at the end of the method declaration?, is it the same like putting it at the beginning?
Second: Why would be a const version and a no const version of operator() needed?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您正在寻找有关 C++ 的优秀资源(包括正确使用
const
的提示),请尝试“Effective C++”。一个关于此的有用网站: JRiddel.org
在 C++ 中,当您声明方法时
const
将其放在方法签名之后,您断言“此方法不会更改正在调用的对象中的任何非可变
实例变量。”返回值之前的 const(例如:operator const char*..." 中的 const)声明它仅返回指向 const 的变量指针char*。(您不能更改
char*
的内容,但可以重新分配指针。)如果您编写了“const char* const ...< /code>" 它将是一个指向常量字符的常量指针。(
const
位于星号之后)。多个版本很有用,因此编译器可以理解这一点:
Chris
If you're looking for a great resource on C++ (including tips on using
const
correctly) try "Effective C++".A useful site about this: JRiddel.org
In C++ when you declare a method
const
by putting it AFTER the method signature you are asserting that "This method will not change any non-mutable
instance variables in the object it is being called on."The
const
before the return value (e.g. the const in:operator const char*..."
) is declaring that it only returns a variable pointer to aconst char*
. (You may not change the contents of thechar*
but you can re-assign the pointer.) If you wrote "const char* const ...
" it would be a constant pointer to constant characters. (Theconst
comes after the star).The multiple versions are useful so the compiler can understand this:
Chris
您应该参考《HIGH·INTEGRITY C++ 编码标准手册》来了解何时建议对类成员使用 const 修饰符:
高完整性 CPP 规则3.1.8:将任何不修改对象的外部可见状态的类成员函数声明为“const”。 (QACPP 4211, 4214)
理由:虽然语言强制按位 const 正确性,但 const 正确性应该被认为是逻辑的,而不是按位的。如果客户端无法确定对象是否因调用该函数而发生更改,则应将成员函数声明为 const。 'mutable' 关键字可用于声明可以在 const 函数中修改的成员数据,仅应在成员数据不影响对象的外部可见状态的情况下使用。
参考有效 C++ 第 21 项;工业强度 C++ 7.13;
You should refer to the "HIGH·INTEGRITY C++ CODING STANDARD MANUAL" for knowing when it is recommended to use the const modifier for class members:
High Integrity CPP Rule 3.1.8: Declare 'const' any class member function that does not modify the externally visible state of the object. (QACPP 4211, 4214)
Justification: Although the language enforces bitwise const correctness, const correctness should be thought of as logical, not bitwise. A member function should be declared const if it is impossible for a client to determine whether the object has changed as a result of calling that function. The 'mutable' keyword can be used to declare member data which can be modified in const functions, this should only be used where the member data does not affect the externally visible state of the object.
Reference Effective C++ Item 21;Industrial Strength C++ 7.13;
开头的Const适用于返回值。最后的 Cons 应用于方法本身。当您将方法声明为“const”时,您是在说您无意修改该方法中类的任何成员变量。编译器甚至会做一些基本检查以确保该方法不会修改成员变量。返回值中的 const 可防止调用者修改返回的值。当您返回由类管理的数据的指针或引用时,这可能很有用。这样做通常是为了避免返回复杂数据的副本,这在运行时可能会很昂贵。
您有两个不同运算符的原因是“const”版本返回一个 const 指针,该指针指向可能是类内部数据的内容。如果类的实例是 const,那么您很可能希望返回的数据也应该是 const。 “非常量”版本仅提供一个方法,当调用者具有该类的非常量实例时,该方法返回可修改的返回值。
Const at the beginning applies to the return value. Const at the end applies to the method itself. When you declare a method as "const" you are saying that you have no intention of modifying any of the member variables of the class in the method. The compiler will even do some basic checks to make sure that the method doesn't modify member variables. The const in the return value prevents the caller from modifying the value that is returned. This can be useful when you return pointers or references to data managed by the class. This is often done to avoid returning copies of complex data which could be expensive at run time.
The reason you have two different operators is that the "const" version returns a const pointer to what is probably data internal to the class. If the instance of the class is const, then chances are you want the data being return should also be const. The "non-const" version just provides a method that returns a modifiable return value when the caller has a non-const instance of the class.
将
const
放在方法声明的末尾表示对象本身或this
是const
而不是返回类型。由于复杂的原因,C++ 允许在
const
上重载方法。这里没有足够的空间来详细介绍。但这里有几个简短的。this
是否为const
都会极大地改变内部方法的绑定。以至于它本质上会变成两个不同的方法体。因此,将其分为两种不同的方法是有意义的。Putting
const
at the end of a method declaration is stating that the object itself, orthis
, isconst
instead of the return type.C++ allows methods to be overloaded on
const
for complicated reasons. Not enough space to go into full detail here. But here are a couple of short ones.const
type. The most straight forward example is when you want to return aconst
value from aconst
method and a non-const value from a normal method.this
isconst
dramatically changes the binding of the internal method. To the point that it would essentially become two different method bodies. Hence it makes sense to break it up into 2 different methods.除了其他答案之外,还有一点要注意:您的示例中没有
operator()
。是转换运算符,这意味着该类的对象可以隐式转换为C风格的字符串,如
operator()
的声明和使用,这意味着您可以使用该类类型的对象sort就像一个函数,看起来像:One note in addition to the other answers: there is no
operator()
in your example.are conversion operators, which mean that objects of the class can be implicitly converted to C-style strings, like
A declaration and usage of
operator()
, which means you can use an object of the class type sort of like a function, would look like:成员函数的常量性。这意味着该函数不能(*)修改您的任何成员变量。这就像在这个函数调用的所有成员变量前面放置一个 const 。这对于您所在班级的客户来说是一个很好的保证,并且还可能有助于编译器优化。
(*) - 另请参阅关键字 mutable。
Member function constness. It means the function can not(*) modify any of your member variables. It's like putting a const in front of all your member variables for this one function call. It's a good guarantee for the clients of your class and may also aid in compiler optimisations.
(*) - see also the keyword mutable.
最后的“const”告诉编译器此方法不会更改任何成员变量 - 在 const 实例上调用此方法是安全的。因此,可以在 const 实例上调用 Save,因为它不会更改该实例。另一方面,加载将更改实例,因此不能在 const 实例上使用。
const 版本的operator() 传回一个const 指针,保证传回的缓冲区不会改变。据推测,这是指向该类的实例变量的指针。对于非常量实例,另一个operator()传回非常量指针。它必须是指向某些内存的指针,即使写入该内存也不会更改实例的内容。
另外,有时查找一下“mutable”关键字。理解这一点将有助于您理解常量正确性的概念。
'const' at the end tells the compiler that this method does not change any member variables - that it is safe to call this method on const instances. So, Save could be called on a const instance, since it won't change that instance. Load on the other hand, will change the instance so can't be used on const instances.
The const version of operator() passes back a const pointer, guaranteeing the buffer passed back won't change. Presumably that's a pointer into a instance variable of the class. For non-const instances, the other operator() passes back a non-const pointer. It would have to be a pointer to some memory that even if written to, wouldn't change the contents of the instance.
Also, look up the 'mutable' keyword sometime. Understanding that will help you understand this idea of const-correctness.
不可以。末尾的
const
意味着可以在声明为const
的对象上调用该方法。开头的const
表示返回值是const
。非 const 版本返回一个非 const 的
char*
。通过修改此char*
,您实际上可以修改该对象(假设char*
是该对象的成员)。由于
const
对象不允许这样做,因此 const 对象会重载operator()
,从而返回const char*
,所以不能通过它修改该对象。No. A
const
at the end means that the method may be called on objects that are declaredconst
. Aconst
at the beginning means that the returned value isconst
.The non-const version returns a
char*
which is not const. By modifying thischar*
you could then in fact modify the object (assuming thechar*
is a member of the object).Since this is not allowed for
const
objects, there's an overload ofoperator()
for const objects, so that aconst char*
is returned, so the object can't be modified through it.