请教如何自定义异常
未解决
现代C++ | mcpp论坛
-
我是一个会一点C++的菜鸟,我需要在自己的代码里面自定义一个异常类,然后我的代码的写法类似于下面这样:
#include <exception> class myException : public std::exception { private: const char * msg; public: myException(const char* message) noexcept : msg(message){} const char * what() const noexcept override{ return msg; } };感觉这样写好像不太对,自己看了一下
std::exception、std::runtime_error、std::logic_error的代码,发现std::runtime_error和std::logic_error都是用__cow_string这个结构体来存储字符串信息的,代码如下:struct __cow_string { union { const char* _M_p; char _M_bytes[sizeof(const char*)]; }; __cow_string(); __cow_string(const std::string&); __cow_string(const char*, size_t); __cow_string(const __cow_string&) _GLIBCXX_NOTHROW; __cow_string& operator=(const __cow_string&) _GLIBCXX_NOTHROW; ~__cow_string(); __cow_string(__cow_string&&) noexcept; __cow_string& operator=(__cow_string&&) noexcept; };貌似只有声明,看不到实现。
所以到这里来请教一下。 -
,
W wlly-lzh 将这个主题转为问答主题