C++异常类扩展语法
C++ exception class extension syntax
我正在在线上课,遇到了一些我不确定我是否理解的语法。
#include <iostream>
#include <exception>
using namespace std;
class derivedexception: public exception {
virtual const char* what() const throw() {
return "My derived exception";
}
} myderivedexception;
int main() {
try {
throw myderivedexception;
}
catch (exception& e) {
cout << e.what() << 'n';
}
}
我的问题是:
virtual const char* what() const throw()
这句话是什么意思?
另外,什么是
} myderivedexception;
在类声明的末尾?
这一行:
virtual const char* what() const throw()
说 what
是一个虚拟方法,它返回指向常量char
的指针(这意味着它可用于返回字符串文字,或通过调用 String::c_str(( 函数获得的 std::string 的内容(,本身是常量,因此它不会修改任何类成员,也不会引发任何异常。
这一行:
} myderivedexception;
创建名为 myderivedexception
的 derivedexception
类的实例。您可能不想这样做,而是抛出一个未命名的异常:
throw derivedexception();
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- QMetaObject invokeMethod的基于函数指针的语法
- 是否可以通过C++扩展强制多个python进程共享同一内存
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 扩展光电二极管探测器以支持多个传感器
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- C++中的VLA,扩展名为std=C++11
- C++异常类扩展语法
- 模板参数包扩展语法的理由
- 扩展现有的 Spirit 语法(AST 和 skipper 的问题)
- 宏扩展中的语法错误
- 我可以让 vim 在没有扩展名的标头上C++语法突出显示吗?
- 比较参数包?扩展语法
- 扩展两个类的参数的语法
- 使用更好的语法/命名扩展特征