自定义异常类的C++语法

eld*_*eko 5 c++ exception custom-exceptions

我是C++的新手,并且发现了以下代码片段,用于从std :: exception扩展的自定义异常.我不理解的唯一部分是: err_msg(msg) {}构造函数定义之后.任何人都可以解释为什么这不是在函数括号中?

class my_exception : public std::exception {
  private:
    std::string err_msg;

  public:
    my_exception(const char *msg) : err_msg(msg) {};
    ~my_exception() throw() {};
    const char *what() const throw() { return this->err_msg.c_str(); };
};
Run Code Online (Sandbox Code Playgroud)

use*_*087 4

该成员err_msg已由初始值设定项列表初始化。

my_exception(const char *msg) : err_msg(msg) {};
//                         here ^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

所以对于构造者来说没什么可做的。


旁注:有一些关于在异常中不使用 std::string的讨论。只需谷歌搜索或查看此处即可