Should I inherit from std::exception?

前端 未结 14 1476
有刺的猬
有刺的猬 2020-11-30 18:56

I\'ve seen at least one reliable source (a C++ class I took) recommend that application-specific exception classes in C++ should inherit from std::exception. I\

14条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 19:42

    There is one problem with inheritance that you should know about is object slicing. When you write throw e; a throw-expression initializes a temporary object, called the exception object, the type of which is determined by removing any top-level cv-qualifiers from the static type of the operand of throw. That could be not what you're expecting. Example of problem you could find here.

    It is not an argument against inheritance, it is just 'must know' info.

提交回复
热议问题