07 2023 档案
摘要:[toc] * 成员函数里用shared_ptr包装this指针,当shared_ptr析构了,就会调用this对象的析构函数,是非常可怕的事情。 ``` class A { void eat() { std::shared_ptr p(this); ptr->status = "吃了"; } }
阅读全文
摘要:[toc] * 原理:浅拷贝,只复制了地址,可能发生两次析构 ``` class Obj { char* str; ~Obj() { delete str; } // 成员变量有指针,析构是必须要写的 }; Obj getInstance() { Obj ret; return ret; // 出了
阅读全文