本文介绍了在C ++中的函数声明结束处的Const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的函数声明中,
const char* c_str ( ) const;
第二个const是什么?
what does the second const do ?
推荐答案
这意味着该方法是一个const方法调用这样的方法不能更改任何实例的数据(除了 mutable
data members),并且只能调用其他const方法。
It means the method is a "const method" A call to such a method cannot change any of the instance's data (with the exception of mutable
data members) and can only call other const methods.
const方法可以在const或非const实例上调用,但非const方法只能调用非const实例。
Const methods can be called on const or non-const instances, but non-const methods can only be called on non-const instances.
struct Foo {
void bar() const {}
void boo() {}
};
Foo f0;
f0.bar(); // OK
fo.boo(); // OK
const Foo f1;
f1.bar(); // OK
f1.boo(); // Error!
这篇关于在C ++中的函数声明结束处的Const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!