Mat*_*ner 4 c++ gcc const exception
我很好奇std::exception::what成员函数为什么const?
class exception
{
public:
exception() throw() { }
virtual ~exception() throw();
/** Returns a C-style character string describing the general cause
* of the current error. */
virtual const char* what() const throw();
};
Run Code Online (Sandbox Code Playgroud)
Jam*_*lis 17
调用what()成员函数不应修改exception对象的可观察状态.
通常,const引用捕获异常.例如,
try {
// ...
}
catch (const std::exception& ex) {
std::cout << ex.what();
}
Run Code Online (Sandbox Code Playgroud)
如果what()成员函数不是const限定的,则此模式将不起作用,因为您无法从catch块中调用它.
如果您不希望在what()调用之前生成要返回的字符串,则可以将字符串具体化为可变成员变量.
| 归档时间: |
|
| 查看次数: |
3178 次 |
| 最近记录: |