I am fairly new to C++ and have found the following code snippet for a custom exception extended from std::exception. The only part I don't understand is the : err_msg(msg) {}
after the constructor definition. Can anyone explain why this is not in the function braces?
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(); };
};
The member err_msg
is already initialized by the initializer list.
my_exception(const char *msg) : err_msg(msg) {};
// here ^^^^^^^^^^^^
So nothing to do for the contructor.
Sidenote: There is a some discussion about not using std::string in exceptions. Just google for it or see here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With