问题描述
为什么我得到 后指明what()。由于它是一个常量方法,它不能做任何可能会修改的东西。 CustomException :: code 不是一个const方法,这意味着它不承诺不修改自己。因此 CustomException :: what 无法调用 CustomException :: code 。
CustomException::what calls CustomException::code. CustomException::what is a const method, as signified by the const after what(). Since it is a const method, it cannot do anything that may modify itself. CustomException::code is not a const method, which means that it does not promise to not modify itself. So CustomException::what can't call CustomException::code.
请注意,const方法不一定与const实例相关。 Foo :: bar 可以将 exc 变量声明为非const并调用const方法,如 CustomException ::什么;这只是意味着 CustomException :: what 承诺不会修改 exc ,但其他代码可能会是。
Note that const methods are not necessarily related to const instances. Foo::bar can declare its exc variable as non-const and call const methods like CustomException::what; this simply means that CustomException::what promises not to modify exc, but other code might.
C ++常见问题有关于。
The C++ FAQ has a bit more information on const methods.
这篇关于g ++:const丢弃限定符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!