原本以为RAII是专指智能指针,看完这篇文章之后我对RAII有了更深的理解,资源的生命周期绑定到unique_ptr这种RAII类属于RAII思想的运用,把资源的生命周期绑定到类中也属于RAII思想的运用。
wlly-lzh
@wlly-lzh
-
从小白的视角探究 vector 第一章补充内容 -
请教如何自定义异常@SPeak 那正确的写法是什么样的呢?我现在是直接继承
std::runtime_error类,如果我不想继承这运行时错误、逻辑错误两个类,想要直接继承自std::exception呢? -
请教如何自定义异常我是一个会一点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; };貌似只有声明,看不到实现。
所以到这里来请教一下。